---
# System prepended metadata

title: LeetCode 練習：2022-12-08

---

# 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
```