Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74c5a33

Browse files
Add Solution.java to problems 0872
1 parent c3ae70f commit 74c5a33

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
12+
List<Integer> rootA = new ArrayList<>();
13+
List<Integer> rootB = new ArrayList<>();
14+
dfs(root1, rootA);
15+
dfs(root2, rootB);
16+
return rootA.equals(rootB);
17+
}
18+
19+
private void dfs(TreeNode root, List<Integer> res) {
20+
if (root != null) {
21+
if (root.left == null && root.right == null) {
22+
res.add(root.val);
23+
}
24+
dfs(root.left, res);
25+
dfs(root.right, res);
26+
}
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /