Skip to content

Commit 10fd019

Browse files
committed
fd
1 parent f816c6b commit 10fd019

File tree

2 files changed

+109
-13
lines changed

2 files changed

+109
-13
lines changed

leetcode/solution/src/SymmetricTree.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
import java.util.Stack;
4+
15
public class SymmetricTree {
26

37
public boolean isSymmetric(TreeNode root) {
@@ -16,4 +20,72 @@ private boolean isSymmetric(TreeNode left, TreeNode right) {
1620
}
1721
return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
1822
}
23+
24+
public boolean isSymmetric2(TreeNode root) {
25+
if (root == null) {
26+
return true;
27+
}
28+
Queue<TreeNode> queue1 = new LinkedList<>();
29+
Queue<TreeNode> queue2 = new LinkedList<>();
30+
queue1.offer(root.left);
31+
queue2.offer(root.right);
32+
while (!queue1.isEmpty() && !queue2.isEmpty()) {
33+
TreeNode left = queue1.poll();
34+
TreeNode right = queue2.poll();
35+
if (left == null && right == null) {
36+
continue;
37+
}
38+
if (left == null || right == null) {
39+
return false;
40+
}
41+
if (left.val != right.val) {
42+
return false;
43+
}
44+
queue1.offer(left.left);
45+
queue1.offer(left.right);
46+
queue2.offer(right.right);
47+
queue2.offer(right.left);
48+
}
49+
return queue1.isEmpty() && queue2.isEmpty();
50+
}
51+
52+
/**
53+
* 非递归写法
54+
*/
55+
public boolean isSymmetric2(TreeNode left, TreeNode right) {
56+
if (left == null && right == null) {
57+
return true;
58+
}
59+
if (left == null || right == null) {
60+
return false;
61+
}
62+
63+
Stack<TreeNode> stack1 = new Stack<TreeNode>();
64+
Stack<TreeNode> stack2 = new Stack<TreeNode>();
65+
66+
while (!stack1.isEmpty() || left != null) {
67+
if (left != null) {
68+
if (right == null) {
69+
return false;
70+
}
71+
if (left.val != right.val) {
72+
return false;
73+
}
74+
stack1.push(left);
75+
stack2.push(right);
76+
left = left.left;
77+
right = right.right;
78+
} else {
79+
if (right != null) {
80+
return false;
81+
}
82+
if (stack2.isEmpty()) {
83+
return false;
84+
}
85+
left = stack1.pop().right;
86+
right = stack2.pop().left;
87+
}
88+
}
89+
return stack2.isEmpty() && right == null;
90+
}
1991
}

leetcode/src/Main.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,48 @@
44

55
public class Main {
66

7-
public TreeNode trimBST(TreeNode root, int L, int R) {
8-
if (root == null) {
9-
return root;
7+
public boolean isSymmetric(TreeNode left, TreeNode right) {
8+
if (left == null && right == null) {
9+
return true;
1010
}
11-
12-
if (root.val > R) {
13-
root.right = null;
14-
return trimBST(root.left, L, R);
11+
if (left == null || right == null) {
12+
return false;
1513
}
1614

17-
if (root.val < L) {
18-
root.left = null;
19-
return trimBST(root.right, L, R);
15+
Stack<TreeNode> stack1 = new Stack<TreeNode>();
16+
Stack<TreeNode> stack2 = new Stack<TreeNode>();
17+
18+
while (!stack1.isEmpty() || left != null) {
19+
if (left != null) {
20+
if (right == null) {
21+
return false;
22+
}
23+
if (left.val != right.val) {
24+
return false;
25+
}
26+
stack1.push(left);
27+
stack2.push(right);
28+
left = left.left;
29+
right = right.right;
30+
} else {
31+
if (right != null) {
32+
return false;
33+
}
34+
if (stack2.isEmpty()) {
35+
return false;
36+
}
37+
left = stack1.pop().right;
38+
right = stack2.pop().left;
39+
}
2040
}
41+
return stack2.isEmpty() && right == null;
42+
}
2143

22-
root.left = trimBST(root.left, L, R);
23-
root.right = trimBST(root.right, L, R);
24-
return root;
44+
public boolean isSymmetric(TreeNode root) {
45+
if (root == null) {
46+
return true;
47+
}
48+
return isSymmetric(root.left, root.right);
2549
}
2650

2751
public static void main(String[] args) {

0 commit comments

Comments
 (0)