669. Trim a Binary Search Tree
Link: https://leetcode.com/problems/trim-a-binary-search-tree/
Description
Given the root
of a binary search tree and the lowest and highest boundaries as low
and high
, trim the tree so that all its elements lies in [low, high]
. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node’s descendant should remain a descendant). It can be proven that there is a unique answer.
Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Approach
Recursion
- If
root
equals to null, return null. - If the value of
root
less thanlow
, call the function on the right subtree and return it. - If the value of
root
greater thanhigh
, call the function on the left subtree and return it. - If the value of
root
is valid, call the function on the left subtree and rightsubtree to check whether the rest of the nodes are valid. - Return the `root``
Iteration
- While
root
is not null, and the value of theroot
is not in[low, high]
:- If the value of
root
less thanlow
, set the right subtree ofroot
toroot
. - If the value of
root
greater thanhigh
, set the left subtree ofroot
toroot
.
- If the value of
- Initialiaze a variable
cur
with valueroot
. - While
cur
is not null:- While the value of the left subtree less than
low
:- Set the right subtree of the left subtree to left subtree.
- Set the left subtree of
cur
tocur
.
- While the value of the left subtree less than
- Reset
root
tocur
. - While
cur
is not null:- While the value of the right subtree greater than
high
:- Set the left subtree of the right subtree to right subtree.
- Set the right subtree of
cur
tocur
.
- While the value of the right subtree greater than
- Return root.
/**
* 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 trimBST(TreeNode root, int low, int high) {
if (root == null)
return null;
if (root.val < low)
return trimBST(root.right, low, high);
if (root.val > high)
return trimBST(root.left, low, high);
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}
// Iteration
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
while (root != null && (root.val < low || root.val > high)) {
if (root.val < low)
root = root.right;
else
root = root.left;
}
TreeNode cur = root;
while (cur != null) {
while (cur.left != null && cur.left.val < low)
cur.left = cur.left.right;
cur = cur.left;
}
cur = root;
while (cur != null) {
while (cur.right != null && cur.right.val > high)
cur.right = cur.right.left;
cur = cur.right;
}
return root;
}
}
108. Convert Sorted Array to Binary Search Tree
Link: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Description
Given an integer array nums
where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
Approach
- Create a recursive function with parameters
num
,start
andend
, the last two parameters are used to denote the start and end of the processing subarray. - If
start
larger than or equal toend
, which means the subarray is empty, return null. - Find the middle point of the subarray and initialize a node with the value of middle point.
- Call the function with updated
start
andend
to create the left subtree (the subarray before the middle point) and right subtree (the subarray after the middle point) of the node. - Return the node.
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 sortedArrayToBST(int[] nums) {
return createBST(nums, 0, nums.length);
}
private TreeNode createBST(int[] nums, int start, int end) {
if (start >= end)
return null;
int mid = (start + end) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = createBST(nums, start, mid);
node.right = createBST(nums, mid + 1, end);
return node;
}
}
538. Convert BST to Greater Tree
Link: https://leetcode.com/problems/convert-bst-to-greater-tree/
Description
Given the root
of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.
As a reminder, a binary search tree is a tree that satisfies these constraints:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
Approach
Recursion
- Initialize a global variable
preVal
(denotes the sum value of previous nodes) to 0. - If
root
is null, return null. - Recursively call on the right subtree of
root
. - Add the value of
root
withpreVal
and set the value ofroot
topreVal
. - Recursively call on the left subtree of
root
. - return
root
.
Iteration
- If
root
is null, return null. - Initialize a TreeNode stack, TreeNode
cur
(denotes the ) toroot
andpreVal
(denotes the sum value of previous nodes) to 0. - While the stack is not empty or
cur
is not null:- If
cur
is not null, pushcur
to the stack and pointcur
to its right child. - Else, pop the top of the stack to
cur
. AddpreVal
to the value ofcur
and then set the new value ofcur
topreVal
. Pointcur
to its left child.
- If
- 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;
* }
* }
*/
// Recusion
class Solution {
int preVal = 0;
public TreeNode convertBST(TreeNode root) {
if (root == null)
return null;
convertBST(root.right);
root.val += preVal;
preVal = root.val;
convertBST(root.left);
return root;
}
}
// Iteration
class Solution {
public TreeNode convertBST(TreeNode root) {
if (root == null)
return root;
Stack<TreeNode> stack = new Stack<>();
int preVal = 0;
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.right;
}
else {
cur = stack.pop();
cur.val += preVal;
preVal = cur.val;
cur = cur.left;
}
}
return root;
}
}