669. Trim a Binary Search Tree
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.
Example 1:
Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]
Example 2:
Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]
Constraints:
- The number of nodes in the tree is in the range
[1, 104]. 0 <= Node.val <= 104- The value of each node in the tree is unique.
rootis guaranteed to be a valid binary search tree.0 <= low <= high <= 104
这题属于做出来了但不知道怎么做出来的。 题目的思考方向应该是基于当前节点。目标是递归返回新树中本位置的节点。那么例2中考虑0节点所在位置的递归条件。就有:
- 本节点小于low时,递归本节点的右子树,返回右子树的递归值。左子树舍弃
- 本节点大于high时,递归本节点的左子树,返回左子树的递归值,右子树舍弃。
- 本节点在中间时,同时递归左右,并将当前节点的左右子树更新为递归结果。
做这题的时候想到一个视角或是思维模式问题。感觉做二叉树的题目时,视角可以是集中在最左下角的带叶子节点,可以考虑前序中序后序遍历问题,这样写的递归往往不带返回值。还有一个视角是当前节点,集中考虑当前节点应返回什么,这样做的递归应返回当前节点。。
/**
* 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 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;
}
}
108. Convert Sorted Array to Binary Search Tree
Easy
10.1K
498
Amazon
Microsoft
Given an integer array nums where the elements are sorted in ascending order, convert it to a
height-balanced
binary search tree.
Example 1:
Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:
Example 2:
Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
Constraints:
1 <= nums.length <= 104-104 <= nums[i] <= 104numsis sorted in a strictly increasing order.
这题就是类似二分法。连续做二叉树的题目,一时不习惯没递归的情况。 边界条件也是类似二分法,左闭右开。
/**
* 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 rcHelper(int[] nums, int left, int right) {
if(left == right) {
return null;
}
int idx = (left + right) / 2;
int cur = nums[idx];
TreeNode node = new TreeNode(cur);
node.left = rcHelper(nums, left, idx);
node.right = rcHelper(nums, idx+1, right);
return node;
}
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) {
return null;
}
return rcHelper(nums, 0, nums.length);
}
}
538. Convert BST to Greater Tree
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.
Example 1:
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input: root = [0,null,1]
Output: [1,null,1]
Constraints:
- The number of nodes in the tree is in the range
[0, 104]. -104 <= Node.val <= 104- All the values in the tree are unique.
rootis guaranteed to be a valid binary search tree.
这题不是标准遍历。类似一个后中序遍历,即中序遍历的反方向。把传统中序遍历的左右遍历顺序倒一下即可。
/**
* 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 {
int accu = 0;
public void rcHelper(TreeNode node) {
if(node == null) {
return;
}
rcHelper(node.right);
int tmp = node.val;
node.val = node.val + accu;
accu += tmp;
//System.out.println(Integer.toString(node.val) + ", " + Integer.toString(accu));
rcHelper(node.left);
}
public TreeNode convertBST(TreeNode root) {
rcHelper(root);
return root;
}
}