#872. Leaf-Similar Trees
var leafSimilar = function(root1, root2) {
function collectLeaves(node, arr) {
if (node === null) return false
if (node.right === null && node.left === null) arr.push(node)
collectLeaves(node.right, arr)
collectLeaves(node.left, arr)
return arr
}
const arr1 = []
const arr2 = []
const tree1 = collectLeaves(root1, arr1)
const tree2 = collectLeaves(root2, arr2)
return JSON.stringify(tree1) === JSON.stringify(tree2)
};
Easy Depth first search question.
ย