Skip to content

Commit 85521d7

Browse files
committed
Time: 0 ms (100%), Space: 41 MB (84.16%) - LeetHub
1 parent b0f3d87 commit 85521d7

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public boolean dfs(TreeNode tnp, TreeNode tnq){
18+
//tree 끝까지 갔는데도 false가 리턴되지 않으면 그동안 모든 노드의 값이 같았단 얘기
19+
if(tnp == null && tnq == null) return true;
20+
21+
//서로 상태가 다른 경우 false 리턴
22+
if(tnp == null || tnq == null) return false;
23+
if(tnp.val != tnq.val) return false;
24+
25+
return dfs(tnp.left, tnq.left) && dfs(tnp.right, tnq.right);
26+
}
27+
28+
public boolean isSameTree(TreeNode p, TreeNode q) {
29+
return dfs(p,q);
30+
}
31+
}

0 commit comments

Comments
 (0)