Binary Tree
題目
226 Invert Binary Tree (Easy)
114 Flatten Binary Tree to Linked List (Medium)
116 Populating Next Right Pointers in Each Node (Medium)
654 Maximum Binary Tree (Medium)
Construct Binary Tree from
105 Construct Binary Tree from Preorder and Inorder Traversal (Medium)
106 Construct Binary Tree from Inorder and Postorder Traversal (Medium)
889 Construct Binary Tree from Preorder and Postorder Traversal (Medium)
652 Find Duplicate Subtrees (Medium)
*297 Serialize and Deserialize Binary Tree (Hard)
236 Lowest Common Ancestor of a Binary Tree (Medium)
1373 Maximum Sum BST in Binary Tree (Hard)
104 Maximum Depth of Binary Tree (Easy)
543 Diameter of Binary Tree (Easy)
traverse
94 Binary Tree Inorder Traversal (Easy)
102 Binary Tree Level Order Traversal (Medium)
107 Binary Tree Level Order Traversal II (Medium)
103 Binary Tree Zigzag Level Order Traversal (Medium)
144 Binary Tree Preorder Traversal (Easy)
145 Binary Tree Postorder Traversal (Easy)
Morris Traversal
O(1)
spaceO(n) time
100 Same Tree (Easy)
104 Maximum Depth of Binary Tree (Easy)
111 Minimum Depth of Binary Tree (Easy)
222 Count Complete Tree Nodes (Medium)
501 Find Mode in Binary Search Tree (Easy)
N-ary Tree
559 Maximum Depth of N-ary Tree (Easy)
589 N-ary Tree Preorder Traversal (Easy)
590 N-ary Tree Postorder Traversal (Easy)
429 N-ary Tree Level Order Traversal
965 Univalued Binary Tree (Easy)
341 Flatten Nested List Iterator (Medium)
Path Sum
- 112 Path Sum (Easy)
- 113 Path Sum II (Medium)
- *437 Path Sum III (Medium)
- 129 Sum Root to Leaf Numbers (Medium)
- *124 Binary Tree Maximum Path Sum (Hard)
- 687 Longest Univalue Path
[補充]
- 430 Flatten a Multilevel Doubly Linked List (Medium)
- 117 Populating Next Right Pointers in Each Node II (Medium)
- 637 Average of Levels in Binary Tree
- 110 Balanced Binary Tree
- 814 Binary Tree Pruning
- 617 Merge Two Binary Trees (Easy)
- 117 Populating Next Right Pointers in Each Node II (Medium)
觀念
快速排序是二元樹的前序遍歷,合併排序是二元樹的後序遍歷。
1 | // 快速排序 |
寫有關於樹的演算法,或是說寫有關於遞迴演算法,關鍵一直都是必須搞清楚明確函數的定義是什麼,相信這個定義,不要跳入遞迴細節。
遍歷樹 框架
1 | /* 二元樹遍歷框架 */ |
Binary Search Tree
Binary Search Tree BST 中序遍歷是有序的
BST 框架
題目
- 230 Kth Smallest Element in a BST (Medium)
- 538 Convert BST to Greater Tree (Medium)
- 1038 Binary Search Tree to Greater Sum Tree (Medium)
operation
450 Delete Node in a BST (Medium)
701 Insert into a Binary Search Tree (Medium)
700 Search in a Binary Search Tree (Easy)
98 Validate Binary Search Tree (Medium)
96 Unique Binary Search Trees (Medium)
95 Unique Binary Search Trees II (Medium)
1373 Maximum Sum BST in Binary Tree (Hard)
501 Find Mode in Binary Search Tree (Easy)
530 Minimum Absolute Difference in BST (Easy)
783 Minimum Distance Between BST Nodes (Easy)
235 Lowest Common Ancestor of a Binary Search Tree (Easy)
觀念
判斷 BST 的合法性
1 |
|
搜尋一個數
1 | // option 1 brute force |
插入一個數,涉及到“改”,就要返回樹的結構
1 | TreeNode insertIntoBST(TreeNode root, int val) { |
刪除一個數,先“查“再”改“。找到後判斷是否有子節點,及是否破壞結構
1 | // 抽象 框架 |
建造最大二元樹
1 | TreeNode constructMaximumBinaryTree(int[] nums) { |
不同的二元搜尋樹,求樹的數量、列出每棵樹
1 |
|
Graph 圖,用adjacent linked list 、adjacent matrix 存儲
遍歷圖的框架
1 | /* 多叉树遍历框架 */ |
圖的所有路徑
1 | // 记录所有路径 |
二元搜尋數子樹的最大鍵值總和
- 確認子樹合法性(BST左小右大)
- 確認把自己加入是否還合法,比較左子樹最大值和柚子樹最小值
- 左右子樹之和