Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Binary Search
- java
- string
- hash table
- 코딩테스트
- database
- 코테
- 자바
- Stack
- simulation
- Tree
- array
- greedy
- two pointers
- bit manipulation
- Method
- Matrix
- 구현
- 파이썬
- implement
- SQL
- Binary Tree
- Number Theory
- Data Structure
- Math
- dynamic programming
- geometry
- sorting
- Class
- Counting
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 101. Symmetric Tree 본문
1. Input
1) TreeNode root
2. Output
1) root가 좌우대칭 트리라면 true, 아니면 false를 반환
3. Constraint
1) 노드 수의 범위는 [1, 1000]이다.
2) -100 <= Node.val <= 100
4. Example
Input: root = [1,2,2,3,4,4,3] -> Output: true
Input: root = [1,2,2,null,3,null,3] -> Output: false
5. Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root.left==null){
if(root.right==null) return true;
else return false;
} else if(root.right==null) return false;
else{
Stack<TreeNode> ls = new Stack<>();
Stack<TreeNode> rs = new Stack<>();
ls.push(root.left); rs.push(root.right);
while(!ls.empty() && !rs.empty()){
TreeNode ln = ls.pop();
TreeNode rn = rs.pop();
if(ln==null){
if(rn==null) continue;
else return false;
} else if(rn==null) return false;
if(ln.val!=rn.val) return false;
ls.push(ln.left); ls.push(ln.right);
rs.push(rn.right); rs.push(rn.left);
}
if(ls.size()==0 && rs.size()==0) return true;
else return false;
}
}
}
- 13%, 61%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 28289. 과 조사하기 (0) | 2023.07.18 |
---|---|
[LeetCode/Easy] 108. Convert Sorted Array to Binary Search Tree (0) | 2023.07.18 |
[LeetCode/Easy] 83. Remove Duplicates from Sorted List (0) | 2023.07.17 |
[LeetCode/Easy] 35. Search Insert Position (0) | 2023.07.17 |
[백준 온라인 저지] 1904. 01타일 (0) | 2023.07.14 |