235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
Example:
1
2
3
4
5
6
7
_______6______
/
___2__ ___8__
/ /
0 _4 79
/
35
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution{
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
if(root == null){
returnnull;
}
if(root.val < p.val && root.val < q.val){
return lowestCommonAncestor(root.right, p, q);
}
elseif(root.val > p.val && root.val > q.val){
return lowestCommonAncestor(root.left, p, q);
}
return root;
}
}
98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Note: Time complexity should be O(height of tree).
Example:
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
root = [5,3,6,2,4,null,7]
key = 3
5
/
36
/
247
Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
5
/
46
/
27
Another valid answer is [5,2,6,null,4,null,7].
5
/
26
47
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Example:
1
2
3
4
5
6
7
8
9
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/
-39
/ /
-105
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
classSolution{
public TreeNode sortedArrayToBST(int[] nums){
if(nums == null){
returnnull;
}
return helper(nums, 0, nums.length-1);
}
private TreeNode helper(int []A, int lo, int hi){
if(lo > hi){
returnnull;
}
int mid = (lo + hi) / 2;
TreeNode node = new TreeNode(A[mid]);
node.left = helper(A, lo, mid-1);
node.right = helper(A, mid+1, hi);
return node;
}
}
230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
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
classSolution
{ privatestaticint count = 0;
privatestaticint res = 0;
publicintkthSmallest(TreeNode root, int k){
if(root == null){
return0;
}
count = k;
helper(root, k);
return res;
}
publicvoidhelper(TreeNode node, int k){
if(node.left != null){
helper(node.left, k);
}
if(--count == 0){
res = node.val;
return;
}
if(node.right != null){
helper(node.right, count);
}
}
}
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
1
2
3
4
5
6
7
_______3______
/
___5__ ___1__
/ /
6 _2 08
/
74
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.
近期评论