Inorder
应用场景:
- BST适用于升序的序列
230.Kth Smallest Element in a BST(BST & Inorder
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Example 1:
1 | Input: root = [3,1,4,null,2], k = 1 |
Example 2:
1 | Input: root = [5,3,6,2,4,null,null,1], k = 3 |
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Constraints:
- The number of elements of the BST is between
1
to10^4
. - You may assume
k
is always valid,1 ≤ k ≤ BST's total elements
.
1 | /** |
Postorder
应用场景:
- 子模块
- 子树
- 从底向上
124.Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [-10,9,20,null,null,15,7] |
1 | /** |
104.Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
1 | 3 |
return its depth = 3.
1 | /** |
第九天LeetCode题目(树的Inorder和Postorder)▼;)
235. Lowest Common Ancestor of a Binary Search Tree(BST)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
1 | Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 |
Example 2:
1 | Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 |
Note:
- All of the nodes’ values will be unique.
- p and q are different and both values will exist in the BST.
1 | class Solution { |
270. Closest Binary Search Tree Value(BST)付费题
找出BST中离Target值最接近的节点
1 | public int ClosestValue(TreeNode root, double target) { |
173. Binary Search Tree Iterator(BST & Inorder)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next()
will return the next smallest number in the BST.
Example:
1 | BSTIterator iterator = new BSTIterator(root); |
Note:
next()
andhasNext()
should run in average O(1) time and uses O(h) memory, where h is the height of the tree.You may assume that
next()
call will always be valid, that is, there will be at least a next smallest number in the BST whennext()
is called.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30//中序遍历从小到大递增
class BSTIterator {
private TreeNode cur;
private Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
cur = root;
stack = new Stack<>();
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
if (!stack.isEmpty() || cur != null) {
return true;
}
return false;
}
/** @return the next smallest number */
public int next() {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
int val = cur.val;
cur = cur.right;
return val;
}
}
285. Inorder Successor in BST(BST & Inorder)
在BST中找给定节点P的下一个节点是什么
1 | public TreeNode InorderSuccessor(TreeNode root, TreeNode p) { |
99. Recover Binary Search Tree(BST & Inorder)
Share
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Example 1:
1 | Input: [1,3,null,null,2] |
Example 2:
1 | Input: [3,1,4,null,null,2] |
Follow up:
- A solution using O(n) space is pretty straight forward.
- Could you devise a constant space solution?
1 | class Solution { |
124. Binary Tree Maximum Path Sum(Postorder)
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [-10,9,20,null,null,15,7] |
1 | //找左边最大路径或者右边最大路径和拐点(根节点)的值 |
250. Count Univalue Subtrees(Postorder)付费题
有多少个Subtree有同样的val
HINT:dango::单独的叶子节点也算一个子树
1 | //PostOrder |
236.Lowest Common Ancestor of a Binary Tree(Postorder)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
Example 1:
1 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
Example 2:
1 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
Note:
- All of the nodes’ values will be unique.
- p and q are different and both values will exist in the binary tree.
1 | //Preorder是从上到下,Postorder是从下到上 |
366. Find Leaves of Binary Tree(Postorder)付费题
找出BST中所有的叶子节点
[4, 5, 3] [2] [1]
1 | public List<List<Integer>> findLeaves (TreeNode root) { |