-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricTree_hard.java
More file actions
executable file
·43 lines (38 loc) · 1.11 KB
/
SymmetricTree_hard.java
File metadata and controls
executable file
·43 lines (38 loc) · 1.11 KB
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
package code.coder.lee.easy;
import code.coder.lee.common.GenerateTreeTool;
import code.coder.lee.common.TreeNode;
import java.util.*;
/**
* Created by bcc on 16/3/28.
*/
public class SymmetricTree_hard {
/**
* 中序遍历不行!!!!!
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isMirror(root.left, root.right);
}
public boolean isMirror(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.val != right.val) {
return false;
}
return isMirror(left.left, right.right) && isMirror(left.right, right.left);
}
public static void main(String[] args) {
Integer[] nums = {1, 2, 2};
TreeNode root = GenerateTreeTool.generateTree(nums);
SymmetricTree_hard symmetricTree_hard = new SymmetricTree_hard();
System.out.println(symmetricTree_hard.isSymmetric(root));
}
}