#872. Leaf-Similar Trees

ยท

1 min read

https://leetcode.com/problems/leaf-similar-trees/description/?envType=daily-question&envId=2024-01-09

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.

ย