码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 872. Leaf-Similar Trees

时间:2019-04-23 09:40:50      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:bool   span   init   ==   efi   tac   and   tree node   node   

找出叶子节点即可.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
        leaf1,leaf2=[],[]
        stack=[root1]
        while stack:
            node=stack.pop()
            if (not node.left) and (not node.right):
                leaf1.append(node.val)
            else:
                if node.right:
                    stack.append(node.right)
                if node.left:
                    stack.append(node.left)
        stack=[root2]
        while stack:
            node=stack.pop()
            if (not node.left) and (not node.right):
                leaf2.append(node.val)
            else:
                if node.right:
                    stack.append(node.right)
                if node.left:
                    stack.append(node.left)
        return leaf1==leaf2
        
        
        

 

Leetcode 872. Leaf-Similar Trees

标签:bool   span   init   ==   efi   tac   and   tree node   node   

原文地址:https://www.cnblogs.com/zywscq/p/10754199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!