from typing import Optional
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
def is_leaf_similar(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    def dfs(root):
        if root is None:
            return []
        
        leaves = dfs(root.left) + dfs(root.right)
        
        return leaves or [root.val]
    return dfs(root1) == dfs(root2)| Associated Context | |
|---|---|
| Type | Code Snippet ( .py ) | 
| Associated Tags | Leaf Similarity Depth-First Search (DFS) Tree Traversal Recursive Function Left and Right Children Leaf Value Sequence Internal Node Recursion Algorithm Design numpy Solution class leafSimilar method root1 parameter root2 parameter dfs function TreeNode class Optional type hint leaf value leaf value sequence subtree's leaves | 
| π Custom Description | Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. | 
| π‘ Smart Description | This code snippet defines a class Solution with a function leafSimilar that takes two nodes as input and returns the value of each node in an array. It recursively explore all left and right children, accumulates their leaves by comparing its values to The code snippet defines a class Solution with a method leafSimilar that takes in two binary trees as input. It uses depth-first search (DFS) to traverse the trees and accumulate the leaf values. It then compares the leaf value sequences of both trees and  | 
| π Suggested Searches | Python code to find leaf value sequences of two trees Algorithm to check if a binary tree is the same as an internal node in Python Python function to compare leaves of two trees using DFS How to use dfs() method for finding nodes that are identical with recursive loops and recursion? Code snippet to determine if we have all children from a binary tree Python code to compare leaf value sequences of two trees DFS implementation to find leaf values in a binary tree Python code to check if two trees have similar leaf values Code to compare leaf values of two binary trees using DFS Python code to find leaf values in a binary tree and compare them  | 
| Related Links | https://leetcode.com/problems/leaf-similar-trees/submissions/ https://leetcode.com/problems/leaf-similar-trees/discuss/ https://leetcode.com/problems/leaf-similar-trees/discuss/?currentPage=1&orderBy=hot&query= https://leetcode.com/problems/leaf-similar-trees/ https://www.geeksforgeeks.org/ https://www.geeksforgeeks.org/python-lists/ https://www.geeksforgeeks.org/python-string/ https://www.hackerearth.com/practice/data-structures/trees/binary-search-tree/tutorial/ https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/ https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/ https://www.programcreek.com/python/example/104993/leetcode.leafSimilar  | 
| Related People | Rohan Mallick | 
| Sensitive Information | No Sensitive Information Detected | 
| Shareable Link | https://ca772742-b4e2-4612-af82-29a364c1f0bb.pieces.cloud/?p=aa74429b2f |