LeetCode刷题 Day22
235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) node 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).”
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1
Output: 2
思路:
- 这道题和普通二叉树的LCA问题有相似之处,但是因为是BST,所以要充分利用其特点 如果后序遍历节点在的值是p和q之间的值时 就说明找到了最近公共祖先
代码:
var lowestCommonAncestor = function(root, p, q) {
if (!root) return null;
if (root.val < p.val && root.val < q.val) {
return lowestCommonAncestor(root.right, p, q);
} else if (root.val > p.val && root.val > q.val) {
return lowestCommonAncestor(root.left, p, q);
} else {
return root;
}
};
时间复杂度: 平均O(logN), 最差O(N) 空间复杂度: 平均O(logN), 最差O(N)
701. Insert into a Binary Search Tree
You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Example 1:
Input: root = [4,2,7,1,3], val = 5
Output: [4,2,7,1,3,5]
Explanation: Another accepted tree is:
Example 2:
Input: root = [40,20,60,10,30,50,70], val = 25
Output: [40,20,60,10,30,50,70,null,null,25]
Example 3:
Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
Output: [4,2,7,1,3,5]
思路:
- 根据BST性质,如果root.val > val 搜寻左树 root.val < val 搜寻右树。直到找到空节点为止
代码:
var insertIntoBST = function(root, val) {
if (!root) return new TreeNode(val);
if (root.val > val) root.left = insertIntoBST(root.left, val);
if (root.val < val) root.right = insertIntoBST(root.right, val);
return root;
};
时间复杂度: 平均O(logN), 最差O(N) 空间复杂度: 平均O(logN), 最差O(N)
450. Delete Node in a 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.
Basically, the deletion can be divided into two stages:
- Search for a node to remove.
- If the node is found, delete the node.
Example 1:
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: 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 above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
Example 2:
Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The tree does not contain a node with value = 0.
Example 3:
Input: root = [], key = 0
Output: []
五种情况:
- 找不到节点 最后return null
- 找到在叶子节点 return null
- left && !right return left;
- !left && right return right;
- 有left和right,将root.left 挂到 root.right的最左子树,这样保证仍符合BST结构,清除root.left, root = root.right
代码:
var deleteNode = function(root, key) {
if (!root) return null;
if (root.val > key) {
root.left = deleteNode(root.left, key);
}
if (root.val < key) {
root.right = deleteNode(root.right, key);
}
if (root.val === key) {
if (!root.left && !root.right) return null;
else if (root.left && !root.right) return root.left;
else if (!root.left && root.right) return root.right;
else {
const left = root.left;
let curr = root.right;
root.left = null;
while (curr.left) {
curr = curr.left;
}
curr.left = left;
root = root.right;
return root;
}
}
return root;
};
时间复杂度: 平均O(logN), 最差O(N) 空间复杂度: 平均O(logN), 最差O(N)