235. Lowest Common Ancestor of a Binary Search Tree
Link: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
Description
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).”
Approach
The first node whose value in [p.val, q.val]
is the LCA.
Recursion
- If the value of
root
less than bothp
andq
, call the function onroot.right
and return. - Else If the value of
root
greater than bothp
andq
, call the function onroot.left
and return. - Else, return the
root
.
Iterartion
- While
root
is not null:- If the value of
root
less than bothp
andq
, set the right subtree ofroot
toroot
. - Else If the value of
root
greater than bothp
andq
, set the left subtree ofroot
toroot
. - Else, return
root
.
- If the value of
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// Recursion
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root.val > p.val && root.val > q.val)
return lowestCommonAncestor(root.left, p, q);
else if (root.val < p.val && root.val < q.val)
return lowestCommonAncestor(root.right, p, q);
return root;
}
}
// Iteration
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while (root != null) {
if (root.val > p.val && root.val > q.val)
root = root.left;
else if (root.val < p.val && root.val < q.val)
root = root.right;
else
return root;
}
return null;
}
}
701. Insert into a Binary Search Tree
Link: https://leetcode.com/problems/insert-into-a-binary-search-tree/
Description
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.
Approach
Recursion
- If
root
is null, which means that the suitable place to insert is found, return the new node with the givenval
. - If the value of
root
less thanval
, call the function on the right subtree ofroot
. - Else, call the function on the left subtree of
root
. - Return
root
.
Iteration
- If
root
is null, which means the tree is empty, return the new node with the givenval
. - Initialize two variables
cur
asroot
andpre
as null, to store the current node and previous node respectively. - While
cur
is not null:- Set
cur
topre
. - If the value of
cur
less thanval
, set the right subtree ofcur
tocur
. - If the value of
cur
greater thanval
, set the left subtree ofcur
tocur
.
- Set
- If the value of
pre
less thanval
, set the node with valueval
as the left subtree ofpre
’ - Else, set that as the right subtree of
pre
.
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
// Recursion
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null)
return new TreeNode(val);
if (root.val < val)
root.right = insertIntoBST(root.right, val);
else
root.left = insertIntoBST(root.left, val);
return root;
}
}
// Iteration
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null)
return new TreeNode(val);
TreeNode cur = root;
TreeNode pre = null;
while (cur != null) {
pre = cur;
if (cur.val > val)
cur = cur.left;
else
cur = cur.right;
}
TreeNode node = new TreeNode(val);
if (val < pre.val)
pre.left = node;
else
pre.right = node;
return root;
}
}
450. Delete Node in a BST
Link: https://leetcode.com/problems/delete-node-in-a-bst/
Description
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.
Approach
- If
root
is null, return null. - If the value of
root
less than key, call the function on the right subtree ofroot
. - Else if the value of
root
greater than key, call the function on the left subtree ofroot
. - Else, if the left subtree is null, return the right subtree. Else if the right subtree is null, return the left subtree. Else, find the leftmost subtree of the right subtree of
root
, move the left subtree ofroot
to the left subtree of the leftmost subtree of the right subtree ofroot
, return the right subtree ofroot
. - Return
root
.
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null)
return null;
if (root.val < key)
root.right = deleteNode(root.right, key);
else if (root.val > key)
root.left = deleteNode(root.left, key);
else {
if (root.left == null)
return root.right;
if (root.right == null)
return root.left;
TreeNode cur = root.right;
while (cur.left != null)
cur = cur.left;
cur.left = root.left;
root = root.right;
return root;
}
return root;
}
}