美文网首页
LeetCode - Leaf-Similar Trees

LeetCode - Leaf-Similar Trees

作者: Andy1944 | 来源:发表于2019-07-18 15:45 被阅读0次

Leaf-Similar Trees - LeetCode

Solution

class Solution {
    func leafSimilar(_ root1: TreeNode?, _ root2: TreeNode?) -> Bool {
        return getLeaf(root1) == getLeaf(root2)
    }
    
    func getLeaf(_ root: TreeNode?) -> [Int] {
        if let root = root {
            if root.left == nil && root.right == nil {
                return [root.val]
            } else {
                return getLeaf(root.left) + getLeaf(root.right)
            }
        } else {
            return []
        }
    }
}

解题思路

取出两棵树的叶子节点,比较两个数组时候相等

相关文章

网友评论

      本文标题:LeetCode - Leaf-Similar Trees

      本文链接:https://www.haomeiwen.com/subject/oamllctx.html