题目:
请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。
解答:
# 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)