樹形 DFS Tree DFS
46 題(31 題以此為主技法)
🎯 何時用
在樹上需要子樹資訊回傳給父節點(高度、路徑和、是否平衡)。
🧩 模板心法
後序遞迴:先算左右子樹回傳值,再合成本節點答案;必要時用全域變數記錄跨節點最佳。
Easy 15 題
- #108Convert Sorted Array to Binary Search Tree
- #110Balanced Binary Tree
- #543Diameter of Binary Tree
- #94Binary Tree Inorder Traversal
- #112Path Sum
- #144Binary Tree Preorder Traversal
- #145Binary Tree Postorder Traversal
- #606Construct String From Binary Tree
- #617Merge Two Binary Trees
- #783Minimum Distance Between BST Nodes
- #100Same Tree
- #104Maximum Depth of Binary Tree
- #226Invert Binary Tree
- #572Subtree of Another Tree
- #101Symmetric Tree
Medium 29 題
- #98Validate Binary Search Tree
- #105Construct Binary Tree from Preorder and Inorder Traversal
- #230Kth Smallest Element in a BST
- #235Lowest Common Ancestor of a Binary Search Tree
- #236Lowest Common Ancestor of a Binary Tree
- #450Delete Node in a BST
- #1448Count Good Nodes in Binary Tree
- #95Unique Binary Search Trees II
- #96Unique Binary Search Trees
- #106Construct Binary Tree from Inorder and Postorder Traversal
- #129Sum Root to Leaf Numbers
- #337House Robber III
- #427Construct Quad Tree
- #538Convert BST to Greater Tree
- #652Find Duplicate Subtrees
- #669Trim a Binary Search Tree
- #701Insert into a Binary Search Tree
- #894All Possible Full Binary Trees
- #951Flip Equivalent Binary Trees
- #1443Minimum Time to Collect All Apples in a Tree
- #102Binary Tree Level Order Traversal
- #173Binary Search Tree Iterator
- #199Binary Tree Right Side View
- #103Binary Tree Zigzag Level Order Traversal
- #513Find Bottom Left Tree Value
- #662Maximum Width of Binary Tree
- #958Check Completeness of a Binary Tree
- #1376Time Needed to Inform All Employees
- #1993Operations On Tree