LeetCode | 235. Lowest Common Ancestor of a BST, 701. Insert into a BST, 450. Delete Node in a BST

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 both p and q, call the function on root.right and return.
  • Else If the value of root greater than both p and q, call the function on root.left and return.
  • Else, return the root.

Iterartion

  • While root is not null:
    • If the value of root less than both p and q, set the right subtree of root to root.
    • Else If the value of root greater than both p and q, set the left subtree of root to root.
    • Else, return root.

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 given val.
  • If the value of root less than val, call the function on the right subtree of root.
  • 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 given val.
  • Initialize two variables cur as root and pre as null, to store the current node and previous node respectively.
  • While cur is not null:
    • Set cur to pre.
    • If the value of cur less than val, set the right subtree of cur to cur.
    • If the value of cur greater than val, set the left subtree of cur to cur.
  • If the value of pre less than val, set the node with value val as the left subtree of pre
  • 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 of root.
  • Else if the value of root greater than key, call the function on the left subtree of root.
  • 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 of root to the left subtree of the leftmost subtree of the right subtree of root, return the right subtree of root.
  • 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值