-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0100_SameTree.java
33 lines (30 loc) · 1.02 KB
/
0100_SameTree.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
// Problem 100: Same Tree
// https://leetcode.com/problems/same-tree/
// Definition for a binary tree node.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
return dfs(p, q);
}
private boolean dfs(TreeNode r, TreeNode s) {
// 如果兩個都到底了, 回傳true
if (r == null && s == null) return true;
// 如果只有其中一個到底, 回傳false
if (r == null || s == null) return false;
// 如果兩個的值相同, 就繼續比對這兩個的左邊 和 這兩個的右邊, 回傳 and 的結果(要左比左相同, 右比右相同才會是true)
if (r.val == s.val) return dfs(r.right, s.right) && dfs(r.left, s.left);
// 其餘都是false
return false;
}
}