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)
标签:right,leaf,nums,self,872,叶子,root,leetcode,left 来源: https://blog.csdn.net/jqq125/article/details/116597933