Open fockspaces opened 11 months ago
把 DFS 寫法簡化後:
# 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 DFS(root):
if not root:
return []
if not root.left and not root.right:
return [root.val]
return DFS(root.left) + DFS(root.right)
return DFS(root1) == DFS(root2)