-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_872.java
More file actions
31 lines (29 loc) · 1.26 KB
/
Copy pathproblem_872.java
File metadata and controls
31 lines (29 loc) · 1.26 KB
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
/*
872. Leaf-Similar Trees
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. */
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
if (root1 == null || root2 == null) return root1 == root2;
List<Integer> leaves1 = new ArrayList<>();
List<Integer> leaves2 = new ArrayList<>();
collectLeaves(root1, leaves1);
collectLeaves(root2, leaves2);
return leaves1.equals(leaves2);
}
private void collectLeaves(TreeNode root, List<Integer> leaves) {
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left == null && node.right == null) {
leaves.add(node.val);
continue;
}
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
}
}