LC94 Binary Tree Inorder Traversal
Problem
Given a binary tree, return the inorder traversal of its nodes’ values.
Example:
1
2
3
4
5
6
7
8 Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]Follow up: Recursive solution is trivial, could you do it iteratively?
Think
中序遍历,两种方法:递归法和迭代法
Code
- Approach 1
1 | vector<int> inorderTraversal(TreeNode* root) { |
- Approach 2
1 | vector<int> inorderTraversal(TreeNode* root) { |
LC95 Unique Binary Search Trees II
Problem
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Think
递归,选择所有可能的根节点i,然后将left ~ i - 1的部分用递归的方式构成左子树,将i + 1 ~ right的部分用递归的方式构成右子树,最后将所有可能的左右子树和根节点拼接起来,就可以得到所有可能的树的构成
Code
1 | vector<TreeNode*> generateTrees(int n) { |
LC96 Unique Binary Search Trees
Problem
Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?
Example:
1
2
3
4
5
6
7
8
9
10 Input: 3
Output: 5
Explanation:
Given n = 3, there are a total of 5 unique BST's:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Think
DP,对于长度i,取j为中心点,则dp[i] += dp[j - 1] * dp[i - j],1 <= j <= i
Code
1 | int numTrees(int n) { |
LC98 Validate Binary Search Tree
Problem
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
1
2
3
4
5
6 2
/ \
1 3
Input: [2,1,3]
Output: trueExample 2:
1
2
3
4
5
6
7
8
9 5
/ \
1 4
/ \
3 6
Input: [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
Think
递归,函数输入当前节点应该在的范围,如果节点不在这个范围内则返回false;然后递归判断左右子树即可
Code
1 | bool isValidBST(TreeNode* root) { |
LC99 Recover Binary Search Tree
Problem
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Input: [1,3,null,null,2]
1
/
3
\
2
Output: [3,1,null,null,2]
3
/
1
\
2Example 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Input: [3,1,4,null,null,2]
3
/ \
1 4
/
2
Output: [2,1,4,null,null,3]
2
/ \
1 4
/
3Follow up:
- A solution using O(n) space is pretty straight forward.
- Could you devise a constant space solution?
Think
类似线索二叉树的方法,每次到子树的最右边的那个节点的时候就添加一个指针指向下一个应该遍历的节点
Code
1 | void swap(TreeNode* x, TreeNode* y){ |
LC100 Same Tree
Problem
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
1
2
3
4
5
6
7 Input: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
Output: trueExample 2:
1
2
3
4
5
6
7 Input: 1 1
/ \
2 2
[1,2], [1,null,2]
Output: falseExample 3:
1
2
3
4
5
6
7 Input: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
Output: false
Think
递归判断左右子树即可
Code
1 | bool isSameTree(TreeNode* p, TreeNode* q) { |
LC102 Binary Tree Level Order Traversal
Problem
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
For example:
Given binary tree[3,9,20,null,null,15,7]
,
1
2
3
4
5 3
/ \
9 20
/ \
15 7return its level order traversal as:
1
2
3
4
5 [
[3],
[9,20],
[15,7]
]
Think
层序遍历,用队列即可
Code
1 | vector<vector<int>> levelOrder(TreeNode* root) { |
LC103 Binary Tree Zigzag Level Order Traversal
Problem
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree[3,9,20,null,null,15,7]
,
1
2
3
4
5 3
/ \
9 20
/ \
15 7return its zigzag level order traversal as:
1
2
3
4
5 [
[3],
[20,9],
[15,7]
]
Think
层序遍历,用队列即可,然后加上一个flag表示这一层需不需要翻转
Code
1 | vector<vector<int>> zigzagLevelOrder(TreeNode* root) { |
Review
Tree最长用的方法主要有:
- 递归,主要是根据树的结构特性,使得我们很容易找到和树的整体结构类似的子树(大多数时候就是左右子树)
- 使用栈或者队列进行迭代,主要是利用BST的前序中序后续和层序遍历
- 动态规划