您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页补-代码随想录第23天|● 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树

补-代码随想录第23天|● 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树

来源:爱站旅游

● 669. 修剪二叉搜索树

思路一:递归

递归三部曲

代码:

 */
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if(root==null)return root;
        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->left接入符合条件的左孩子
        root.right=trimBST(root.right,low,high); // root->right接入符合条件的右孩子
        return root;
    }
}

思路二:迭代

代码:

class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if(root == null)
            return null;
        // 处理头结点,让root移动到[L, R] 范围内,注意是左闭右闭
        while(root != null && (root.val < low || root.val > high)){
            if(root.val < low)// 小于Low往右走
                root = root.right;
            else// 大于high往左走
                root = root.left;
        }

        TreeNode curr = root;
        
        // 此时root已经在[Low, High] 范围内,处理左孩子元素小于Low的情况
        while(curr != null){
            while(curr.left != null && curr.left.val < low){
                curr.left = curr.left.right;//将左孩子的右孩子直接放入这个位置
            }
            curr = curr.left;
        }
        //返回根节点
        curr = root;

        //此时root已经在[L, R] 范围内,处理右孩子大于R的情况
        while(curr != null){
            while(curr.right != null && curr.right.val > high){
                curr.right = curr.right.left;
            }
            curr = curr.right;
        }
        return root;
    }
}

● 108.将有序数组转换为二叉搜索树

思路:递归

代码;[左闭右闭]

/**
 * 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) {
        TreeNode root = traversal(nums, 0, nums.length - 1);
		return root;
    }
    // 左闭右闭区间[left, right]
	private TreeNode traversal(int[] nums, int left, int right) {
		if (left > right) return null;
        int mid=left+(right-left)/2;
        TreeNode root=new TreeNode(nums[mid]);
        root.left=traversal(nums,left,mid-1);
        root.right=traversal(nums,mid+1,right);
        return root;
    }
}

● 538.把二叉搜索树转换为累加树


思路:

递归 代码:

/**
 * 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 TreeNode convertBST(TreeNode root) {
        convertBST1(root);
        return root;
    }
    //右中左
    public void convertBST1(TreeNode root) {
        if (root == null) {
            return;
        }
        convertBST1(root.right);
        sum+=root.val;
        root.val=sum;
        convertBST1(root.left);
    }
}

迭代-代码

class Solution {
    //DFS iteraion統一迭代法
    public TreeNode convertBST(TreeNode root) {
        int pre = 0;
        Stack<TreeNode> stack = new Stack<>();
        if(root == null) //edge case check
            return null;

        stack.add(root);

        while(!stack.isEmpty()){
            TreeNode curr = stack.peek();
            //curr != null的狀況,只負責存node到stack中
            if(curr != null){ 
                stack.pop();
                if(curr.left != null)       //左
                    stack.add(curr.left);
                stack.add(curr);            //中
                stack.add(null);
                if(curr.right != null)      //右
                    stack.add(curr.right);
            }else{
            //curr == null的狀況,只負責做單層邏輯
                stack.pop();
                TreeNode temp = stack.pop();
                
                pre += temp.val;
                temp.val = pre;
            }
        }
        return root;
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务