樹是階層式的資料結構,由節點和邊組成。二元樹、二元搜尋樹 (BST) 是最常見的變體。遞迴是處理樹問題的核心技巧。
Notes:
- 大多數樹的題目可以用 DFS(前序/中序/後序)或 BFS(層序遍歷)解決
- BST 的中序遍歷會產生有序序列,這個性質非常有用
- 遞迴解法要注意 base case 和回傳值的設計
跨倉庫導讀#
- 對應理論章節:樹 ↗
#94
Binary Tree Inorder Traversal
#95
Unique Binary Search Trees II
#96
Unique Binary Search Trees
#98
Validate Binary Search Tree
#100
Same Tree
#101
Symmetric Tree
#102
Binary Tree Level Order Traversal
#103
Binary Tree Zigzag Level Order Traversal
#104
Maximum Depth of Binary Tree
#105
Construct Binary Tree from Preorder and Inorder Traversal
#106
Construct Binary Tree from Inorder and Postorder Traversal
#108
Convert Sorted Array to Binary Search Tree
#110
Balanced Binary Tree
#112
Path Sum
#124
Binary Tree Maximum Path Sum
#129
Sum Root to Leaf Numbers
#144
Binary Tree Preorder Traversal
#145
Binary Tree Postorder Traversal
#173
Binary Search Tree Iterator
#199
Binary Tree Right Side View
#226
Invert Binary Tree
#230
Kth Smallest Element in a BST
#235
Lowest Common Ancestor of a Binary Search Tree
#297
Serialize and Deserialize Binary Tree
#337
House Robber III
#427
Construct Quad Tree
#450
Delete Node in a BST
#513
Find Bottom Left Tree Value
#538
Convert BST to Greater Tree
#543
Diameter of Binary Tree
#572
Subtree of Another Tree
#606
Construct String From Binary Tree
#617
Merge Two Binary Trees
#652
Find Duplicate Subtrees
#662
Maximum Width of Binary Tree
#669
Trim a Binary Search Tree
#701
Insert into a Binary Search Tree
#783
Minimum Distance Between BST Nodes
#894
All Possible Full Binary Trees
#951
Flip Equivalent Binary Trees
#958
Check Completeness of a Binary Tree
#1376
Time Needed to Inform All Employees
#1443
Minimum Time to Collect All Apples in a Tree
#1448
Count Good Nodes in Binary Tree
#1993
Operations On Tree