#LeetCode 669. Trim a Binary Search Tree
# LeetCode 669. 视频讲解:你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树_哔哩哔哩_bilibili
递归三部曲:
1. 确定参数以及返回值:有TreeNode 返回值,可以通过递归函数的返回值来移除节点。
2. 确定终止条件:空节点返回。
3. 确定单层递归逻辑。
如果当前节点的值不在[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) {
TreeNode right = trimBST(root.right, low, high);
return right;
}
if (root.val > high) {
TreeNode left = trimBST(root.left, low, high);
return left;
}
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}
#LeetCode 108. Convert Sorted Array to Binary Search Tree
# LeetCode 108. 视频讲解:构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树_哔哩哔哩_bilibili
根据数组的内容来分割二叉树,如果数组长度为奇数那么以中间节点为根节点,如果数组长度为偶数,那么左边右边都可以作为根节点,只是构造出的二叉树左右孩子位置不同,所以答案不唯一。
代码中不完善的地方:mid 数值可能存在越界。更优写法int mid = left + ((right - left) / 2); 可以有效的防止数组越界。
递归法(左闭右闭)代码:
/**
* 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 traversal(nums, 0, nums.length - 1);
}
public TreeNode traversal(int[] nums, int low, int high) {
if (low > high) {
return null;
}
int mid = (low + high) / 2;
TreeNode root = new TreeNode (nums[mid]);
root.left = traversal(nums, low, mid - 1);
root.right = traversal(nums, mid + 1, high);
return root;
}
}
#LeetCode 538. Convert BST to Greater Tree
#LeetCode 538. 视频讲解:普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树_哔哩哔哩_bilibili
递归三部曲:
1. 确定参数以及返回值:返回void ,参数为TreeNode 的root 。
2. 确定终止条件:当前遍历的节点如果为空时,则终止遍历。
3. 确定单层递归逻辑:使用右孩子、中节点(如何处理的逻辑)、左孩子的遍历顺序。
使用迭代的方法中,是通过栈来保存各个节点信息的,在当前节点重新加入栈的同时加入一个null 标记,以标记该节点的值需要更新。
递归法代码:
/**
* 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 pre = 0;
public TreeNode convertBST(TreeNode root) {
traversal(root);
return root;
}
public void traversal(TreeNode cur) {
if (cur == null) {
return;
}
traversal(cur.right);
cur.val += pre;
pre = cur.val;
traversal(cur.left);
}
}
迭代法代码:
/**
* 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 convertBST(TreeNode root) {
int pre = 0;
Stack<TreeNode> stack = new Stack<>();
if(root == null) {
return null;
}
stack.add(root);
while(!stack.isEmpty()) {
TreeNode cur = stack.peek();
if (cur != null) {
stack.pop();
if (cur.left != null) {
stack.add(cur.left);
}
stack.add(cur);
stack.add(null);
if (cur.right != null) {
stack.add(cur.right);
}
}
else {
stack.pop();
TreeNode temp = stack.pop();
temp.val += pre;
pre = temp.val;
}
}
return root;
}
}