- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSameTree.java
More file actions
Latest commit
executable file
·25 lines (21 loc) · 682 Bytes
/
Copy pathSameTree.java
File metadata and controls
executable file
·25 lines (21 loc) · 682 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
/**
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Hide Tags Tree Depth-first Search
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
publicclassSameTree {
publicbooleanisSameTree(TreeNodep, TreeNodeq) {
if (p == q) returntrue;
if (p == null ^ q == null) returnfalse;
returnp.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}