File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+ public boolean isSymmetric(TreeNode root) {
18
+ if(root == null) return true;
19
+ Queue<TreeNode> q = new LinkedList<>();
20
+ q.add(root);
21
+ while(!q.isEmpty()){
22
+ int s = q.size();
23
+ List<Integer> lvl = new ArrayList<>();
24
+ for(int i = 0; i<s; i++){
25
+ TreeNode cur = q.poll();
26
+ if(cur.left != null){
27
+ lvl.add(cur.left.val);
28
+ q.add(cur.left);
29
+ }
30
+ else{
31
+ lvl.add(-1234231);
32
+ }
33
+ if(cur.right != null){
34
+ lvl.add(cur.right.val);
35
+ q.add(cur.right);
36
+ }
37
+ else{
38
+ lvl.add(-1234231);
39
+ }
40
+ }
41
+ //System.out.println(lvl);
42
+ if(!isPal(lvl)){
43
+ return false;
44
+ }
45
+ }
46
+ return true;
47
+ }
48
+ public boolean isPal(List<Integer> lvl){
49
+ for(int i = 0; i<lvl.size()/2; i++){
50
+ if(!lvl.get(i).equals(lvl.get(lvl.size()-i-1))){
51
+ //System.out.println(lvl.get(i) + " " + lvl.get(lvl.size()-i-1));
52
+ return false;
53
+ }
54
+ }
55
+ return true;
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments