-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
67 lines (55 loc) · 1.68 KB
/
Solution.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package Tree.easy.No_101_Symmetric_Tree;
import Tree.TreeNode;
import java.util.LinkedList;
import java.util.Queue;
/**
* FileName: Solution
* Author: EdisonLi的家用MacBook Pro
* Date: 2019-03-24 04:44
* Description: Symmetric Tree
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
* But the following [1,2,2,null,3,null,3] is not:
* 1
* / \
* 2 2
* \ \
* 3 3
* Note:
* Bonus points if you could solve it both recursively and iteratively.
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root,root);
}
//递归判断 左子树 等于 右子数
private boolean isMirror(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) return true;
if (root1 == null || root2 == null) return false;
return (root1.val == root2.val) && isMirror(root1.left , root2.right) && isMirror(root1.right,root2.left);
}
public boolean isSymmetric2(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
queue.add(root);
while (!queue.isEmpty()){
TreeNode t1 = queue.poll();
TreeNode t2 = queue.poll();
if (t1 == null && t2 == null) continue;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
queue.add(t1.left);
queue.add(t2.right);
queue.add(t1.right);
queue.add(t2.left);
}
return true;
}
}