[669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
```html
Input:
3
/ \
0 4
\
2
/
1
L = 1
R = 3
Output:
3
/
2
/
1
```
二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。
只保留值在 L \~ R 之间的节点
```java
publicTreeNodetrimBST(TreeNoderoot,intL,intR){
if(root==null)returnnull;
if(root.val>R)returntrimBST(root.left,L,R);
if(root.val<L)returntrimBST(root.right,L,R);
root.left=trimBST(root.left,L,R);
root.right=trimBST(root.right,L,R);
returnroot;
}
```
**从有序数组中构造二叉查找树**
[108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
```java
publicTreeNodesortedArrayToBST(int[]nums){
returntoBST(nums,0,nums.length-1);
}
privateTreeNodetoBST(int[]nums,intsIdx,inteIdx){
if(sIdx>eIdx)returnnull;
intmIdx=(sIdx+eIdx)/2;
TreeNoderoot=newTreeNode(nums[mIdx]);
root.left=toBST(nums,sIdx,mIdx-1);
root.right=toBST(nums,mIdx+1,eIdx);
returnroot;
}
```
**二叉查找树的最近公共祖先**
[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
```html
_______6______
/ \
___2__ ___8__
/ \ / \
0 4 7 9
/ \
3 5
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
```html
_______3______
/ \
___5__ ___1__
/ \ / \
6 2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
[501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/)
[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
```html
1
\
2
/
2
return [2].
_______6______
/ \
___2__ ___8__
/ \ / \
0 4 7 9
/ \
3 5
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
[230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
递归解法:
```html
_______3______
/ \
___5__ ___1__
/ \ / \
6 2 0 8
/ \
7 4
```java
publicintkthSmallest(TreeNoderoot,intk){
intleftCnt=count(root.left);
if(leftCnt==k-1)returnroot.val;
if(leftCnt>k-1)returnkthSmallest(root.left,k);
returnkthSmallest(root.right,k-leftCnt-1);
}
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.