forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17_TreeisIdentical.java
More file actions
31 lines (29 loc) · 844 Bytes
/
17_TreeisIdentical.java
File metadata and controls
31 lines (29 loc) · 844 Bytes
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
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
public class Solution {
/**
* @param a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
public static boolean isIdentical(TreeNode a, TreeNode b) {
// Write your code here
if(a==null&&b==null)
return true;
else if(a==null||b==null)
return false;
else if(a.val==b.val)
return isIdentical(a.left,b.left)&&isIdentical(a.right,b.right);
return false;
}
public static void main(String[]args){
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(1);
System.out.println(isIdentical(node1,node2));
}
}