https://leetcode.com/problems/leaf-similar-trees/
https://leetcode.com/problems/leaf-similar-trees/discuss/152358/Simple-6-lines-Java-StringBuilder-%2B-traverse-with-explanation
https://leetcode.com/problems/leaf-similar-trees/discuss/152870/Java-using-only-one-Queue-and-dfs-2ms
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.
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> leaves1 = new ArrayList();
List<Integer> leaves2 = new ArrayList();
dfs(root1, leaves1);
dfs(root2, leaves2);
return leaves1.equals(leaves2);
}
public void dfs(TreeNode node, List<Integer> leafValues) {
if (node != null) {
if (node.left == null && node.right == null)
leafValues.add(node.val);
dfs(node.left, leafValues);
dfs(node.right, leafValues);
}
}
https://leetcode.com/problems/leaf-similar-trees/discuss/152358/Simple-6-lines-Java-StringBuilder-%2B-traverse-with-explanation
All you need to do is traverse for both trees and records their leaves as string and then compare the strings.
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
StringBuilder root1Leaves = new StringBuilder(),root2Leaves=new StringBuilder();
traverse(root1,root1Leaves);traverse(root2,root2Leaves);
return root1Leaves.toString().equals(root2Leaves.toString());
}
void traverse(TreeNode root,StringBuilder recordLeaves){
if(root==null) return;
if(root.left==null && root.right==null) recordLeaves.append(root.val+"-");
traverse(root.left, recordLeaves);
traverse(root.right, recordLeaves);
}
X. https://leetcode.com/problems/leaf-similar-trees/discuss/152329/C%2B%2BJavaPython-O(logN)-Space
General methode is that traverse DFS whole tree to a list and compare two lists.
Here I share an idea of comparing node by node using
Here I share an idea of comparing node by node using
O(logN)
space.
Use a
Check leaves one by one, until the end or difference.
Only
stack<TreeNode>
to keep dfs path.dfs(stack)
will return next leaf.Check leaves one by one, until the end or difference.
Only
O(logN)
space for stack, no extra space for comparation.O(1)
is also possible if we can modify the tree. public boolean leafSimilar(TreeNode root1, TreeNode root2) {
Stack<TreeNode> s1 = new Stack<>(), s2 = new Stack<>();
s1.push(root1); s2.push(root2);
while (!s1.empty() && !s2.empty())
if (dfs(s1) != dfs(s2)) return false;
return s1.empty() && s2.empty();
}
public int dfs(Stack<TreeNode> s) {
while (true) {
TreeNode node = s.pop();
if (node.right != null) s.push(node.right);
if (node.left != null) s.push(node.left);
if (node.left == null && node.right == null) return node.val;
}
}
https://leetcode.com/problems/leaf-similar-trees/discuss/152870/Java-using-only-one-Queue-and-dfs-2ms
Deque<Integer> qu = new ArrayDeque<>();
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
dfs(root1);
return check(root2) && qu.isEmpty();
}
private void dfs(TreeNode cur) {
if(cur.left == null && cur.right == null) {
qu.offer(cur.val);
return;
}
if(cur.left != null) {
dfs(cur.left);
}
if(cur.right != null) {
dfs(cur.right);
}
}
private boolean check(TreeNode cur) {
if(cur.left == null && cur.right == null) {
return qu.poll() == cur.val;
}
boolean result = true;
if(cur.left != null) {
result = check(cur.left);
}
if(result && cur.right != null) {
result = check(cur.right);
}
return result;
}