# LeetCode 練習:2022-12-08 ## 872. Leaf-Similar Trees [題目872](https://leetcode.com/problems/leaf-similar-trees/description/) ```python=0 # 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: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: def leafStack(root): if not root: return [] if not root.left and not root.right : return [root.val] leafs = leafStack(root.left) leafs += leafStack(root.right) return leafs if leafStack(root1) == leafStack(root2): return True else: return False ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up