代码随想录训练营 Day23
今日任务
669.修剪二叉搜索树
108.将有序数组转换为二叉搜索树
538. 把二叉搜索树转换为累加树
二叉树总结
669. 修剪二叉搜索树
链接:https://leetcode.cn/problems/trim-a-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 {
public TreeNode traversal(TreeNode root, int low, int high){
if(root == null){
return null;
}
//此时root和root的左孩子都不符合条件
if(root.val < low){
return traversal(root.right, low, high);
}
else if(root.val > high){
return traversal(root.left, low, high);
}
root.left = traversal(root.left, low, high);
root.right = traversal(root.right, low, high);
return root;
}
public TreeNode trimBST(TreeNode root, int low, int high) {
return traversal(root, low, high);
}
}
迭代
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
TreeNode cur = root;
TreeNode pre = null;
//将修剪后根节点的位置先找到
while(root != null && (root.val < low || root.val > high)){
if(root.val < low){
root = root.right;
}
else if(root.val > high){
root = root.left;
}
}
cur = root;
//修剪左子树
//因为root的大小是在low和high之间的
//所以root的左子树中节点的值只有可能小于low
while(cur != null){
while(cur.left != null && cur.left.val < low){
cur.left = cur.left.right;
}
cur = cur.left;
}
cur = root;
//修剪右子树
//因为root的大小是在low和high之间的
//所以root的右子树中节点的值只有可能大于high
while(cur != null){
while(cur.right != null && cur.right.val > high){
cur.right = cur.right.left;
}
cur = cur.right;
}
return root;
}
}
108. 将有序数组转换为二叉搜索树
链接:https://leetcode.cn/problems/convert-sorted-array-to-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 {
public TreeNode buildBTS(int[] nums, int left, int right){
if(left == right){
return new TreeNode(nums[left]);
}
if(left > right){
return null;
}
int mid = left + (right - left + 1) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = buildBTS(nums, left, mid - 1);
root.right = buildBTS(nums, mid + 1, right);
return root;
}
public TreeNode sortedArrayToBST(int[] nums) {
int mid = nums.length / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = buildBTS(nums, 0, mid - 1);
root.right = buildBTS(nums, mid + 1, nums.length - 1);
return root;
}
}
迭代
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
Queue<TreeNode> nodeQue = new LinkedList<TreeNode>();
Queue<Integer> leftQue = new LinkedList<Integer>();
Queue<Integer> rightQue = new LinkedList<Integer>();
TreeNode root = new TreeNode(-1);
nodeQue.offer(root);
leftQue.offer(0);
rightQue.offer(nums.length - 1);
while(!nodeQue.isEmpty()){
TreeNode cur = nodeQue.poll();
int left = leftQue.poll();
int right = rightQue.poll();
int mid = left + ((right - left) >> 1);//位运算一定不要忘记括号!!!
cur.val = nums[mid];
if(left < mid){
cur.left = new TreeNode(-1);
nodeQue.offer(cur.left);
leftQue.offer(left);
rightQue.offer(mid - 1);
}
if(right > mid){
cur.right = new TreeNode(-1);
nodeQue.offer(cur.right);
leftQue.offer(mid + 1);
rightQue.offer(right);
}
}
return root;
}
}
538. 把二叉搜索树转换为累加树
链接:https://leetcode.cn/problems/convert-bst-to-greater-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 sum = 0;
public void convert(TreeNode root){
if(root == null){
return;
}
convert(root.right);
sum += root.val;
root.val = sum;
convert(root.left);
}
public TreeNode convertBST(TreeNode root) {
if(root == null){
return null;
}
convert(root);
return root;
}
}
迭代
class Solution {
int sum = 0;
public TreeNode convertBST(TreeNode root) {
if(root == null){
return null;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null && cur.right != null){
stack.push(cur);
cur = cur.right;
}
if(cur == null){
cur = stack.pop();
}
sum += cur.val;
cur.val = sum;
cur = cur.left;
}
return root;
}
}