leetcode每日一题—872.叶子相似的树

题目:
请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。
leetcode每日一题—872.叶子相似的树
leetcode每日一题—872.叶子相似的树
leetcode每日一题—872.叶子相似的树

解答:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
        nums1=[]
        nums2=[]
        def leaf(root,nums):
            if root and not root.left and not root.right:
                nums.append(root.val)
            if root.left:
                leaf(root.left,nums)
            if root.right:
                leaf(root.right,nums)
            return nums
            
        return leaf(root1,nums1)==leaf(root2,nums2)
上一篇:[codeforces]Codeforces Global Round 1 F. Nearest Leaf


下一篇:Codeforces 786B - Legacy(线段树优化建图)