-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricTree.java
More file actions
39 lines (35 loc) · 1.27 KB
/
Copy pathSymmetricTree.java
File metadata and controls
39 lines (35 loc) · 1.27 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
// way 2, this solution is very similer to Balanced Binary Tree
public class Solution {
public boolean isSymmetric(TreeNode root) {
if( root==null ){
return true;
}
return isMirro( root.left, root.right ) && isMirro( root.right, root.left );
}
private boolean isMirro( TreeNode t1, TreeNode t2 ){
if( t1 == null && t2 == null ){
return true ;
}
if( t1 != null && t2 != null && t1.val == t2.val ){ // lead node
return isMirro( t1.left, t2.right ) && isMirro( t1.right, t2.left );
}
return false;
}
}
// https://github.com/leetcoders/LeetCode-Java/blob/master/SymmetricTree.java
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){ return true; }
return isMirror( root.left , root.right );
}
// create a method to left and right deeply at the same time
private boolean isMirror(TreeNode t1, TreeNode t2 ){
if( t1 == null && t2 == null ){
return true;
}
if( t1 == null && t2 != null || t1 != null && t2 == null || t1.val != t2.val ){
return false;
}
return isMirror( t1.left, t2.right ) && isMirror( t1.right, t2.left );
}
}