Leetcode 872. Leaf-Similar Trees

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 872. Leaf-Similar Trees相关的知识,希望对你有一定的参考价值。

找出叶子节点即可.

# 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的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 872. Leaf-Similar Trees

leetcode 872. 叶子相似的树

LeetCode 872 Leaf-Similar Trees 解题报告

Leetcode 872. Leaf-Similar Trees

LeetCode 872 叶子相似的树[DFS 二叉树] HERODING的LeetCode之路

LeetCode 872. 叶子相似的树