代码随想录五刷day7

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


二叉树

一、力扣226. 翻转二叉树

/**
 * 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 invertTree(TreeNode root) {
        return fun(root);
    }
    public TreeNode fun(TreeNode root){
        if(root == null){
            return root;
        }
        TreeNode t = root.left;
        root.left = root.right;
        root.right = t;
        fun(root.left);
        fun(root.right);
        return root;
    }
}

二、力扣101. 对称二叉树

/**
 * 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 boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return fun(root.left, root.right);
    }
    public boolean fun(TreeNode l, TreeNode r){
        if(l == null && r == null){
            return true;
        }
        if(l == null || r == null){
            return false;
        }
        if(l.val != r.val){
            return false;
        }
        return fun(l.left, r.right) && fun(l.right, r.left);
    }
}

三、力扣100. 相同的树

/**
 * 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 boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

四、力扣572. 另一棵树的子树

/**
 * 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 boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null){
            return false;
        }
        return fun(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }
    public boolean fun(TreeNode p, TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return fun(p.left, q.left) && fun(p.right, q.right);
    }
}

五、力扣104. 二叉树的最大深度

/**
 * 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 res = 0;
    public int maxDepth(TreeNode root) {
        fun(root, 1);
        return res;
    }
    public void fun(TreeNode root, int level){
        if(root == null){
            return ;
        }
        res = Math.max(res, level);
        fun(root.left, level + 1);
        fun(root.right, level + 1);
    }
}

六、力扣559. N 叉树的最大深度

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    int depth = 0;
    public int maxDepth(Node root) {
        fun(root, 1);
        return depth;
    }
    public void fun(Node root, int level){
        if(root == null){
            return ;
        }
        depth = Math.max(depth, level);
        for(Node n : root.children){
            fun(n, level + 1);
        }
    }
}

七、力扣111. 二叉树的最小深度

/**
 * 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 depth = Integer.MAX_VALUE;
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        fun(root, 1);
        return depth;
    }
    public void fun(TreeNode root, int level){
        if(root == null){
            return ;
        }
        if(root.left == null && root.right == null){
            depth = Math.min(depth, level);
        }
        fun(root.left, level + 1);
        fun(root.right, level + 1);
    }
}

八、力扣222. 完全二叉树的节点个数

/**
 * 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 count = 0;
    public int countNodes(TreeNode root) {
        fun(root);
        return count;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        count ++;
        fun(root.left);
        fun(root.right);
    }
}

九、力扣110. 平衡二叉树

/**
 * 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 boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        boolean flag = Math.abs(l - r) > 1 ? false : true;
        return flag && isBalanced(root.left) && isBalanced(root.right);
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        return l > r ? l + 1 : r + 1;
    }
}

十、力扣257. 二叉树的所有路径

/**
 * 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 {
    List<String> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        fun(root);
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        path.add(root.val);
        if(root.left == null && root.right == null){
            String s = "";
            for(int i = 0; i < path.size(); i ++){
                s += path.get(i);
                if(i < path.size()-1){
                    s += "->";
                }
            }
            res.add(s);
        }
        fun(root.left);
        fun(root.right);
        path.remove(path.size()-1);
    }
}

十一、力扣404. 左叶子之和

/**
 * 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 int sumOfLeftLeaves(TreeNode root) {
        fun(root);
        return sum;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        if(root.left != null){
            if(root.left.left == null && root.left.right == null){
                sum += root.left.val;
            }
        }
        fun(root.left);
        fun(root.right);
    }
}

十二、力扣513. 找树左下角的值

/**
 * 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 res = 0, depth = 0;
    public int findBottomLeftValue(TreeNode root) {
        fun(root, 1);
        return res;
    }
    public void fun(TreeNode root, int level){
        if(root == null){
            return;
        }
        if(level > depth){
            depth = level;
            res = root.val;
        }
        fun(root.left, level + 1);
        fun(root.right, level + 1);
    }
}

十三、力扣112. 路径总和

/**
 * 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 boolean hasPathSum(TreeNode root, int targetSum) {
        return fun(root, targetSum, 0);
    }
    public boolean fun(TreeNode root, int targetSum, int sum){
        if(root == null){
            return false;
        }
        boolean mid = false;
        if(root.left == null && root.right == null){
            mid = targetSum == sum + root.val ? true : false;
        }
        boolean l = fun(root.left, targetSum, sum + root.val);
        boolean r = fun(root.right, targetSum, sum + root.val);
        return mid || l || r;
    }
}

十四、力扣113. 路径总和 II

/**
 * 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 {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        fun(root, targetSum, 0, new ArrayList<>());
        return res;
    }
    public void fun(TreeNode root, int targetSum, int sum, List<Integer> path){
        if(root == null){
            return;
        }
        path.add(root.val);
        if(root.left == null && root.right == null){
            if(targetSum == root.val + sum){
                res.add(new ArrayList<>(path));
            }
        }
        if(root.left != null){
            fun(root.left, targetSum, sum + root.val, path);
            path.remove(path.size()-1);
        }
        if(root.right != null){
            fun(root.right, targetSum, sum + root.val, path);
            path.remove(path.size()-1);
        }
    }
}

十五、力扣106. 从中序与后序遍历序列构造二叉树

/**
 * 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 {
    Map<Integer,Integer> mapA = new HashMap<>();
    Map<Integer,Integer> mapB = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i = 0; i < inorder.length; i ++){
            mapA.put(inorder[i],i);
        }
        return fun(inorder, postorder,0,inorder.length-1,0,postorder.length-1);
    }
    public TreeNode fun(int[] inorder, int[] postorder, int midStart, int midEnd,int postStart, int postEnd){
        if(midStart > midEnd){
            return null;
        }
        TreeNode cur = new TreeNode(postorder[postEnd]);
        int l = mapA.get(postorder[postEnd]);
        cur.left = fun(inorder,postorder,midStart,l-1,postStart,postStart+l-midStart-1);
        cur.right = fun(inorder,postorder,l+1,midEnd,postEnd-(midEnd-l),postEnd-1);
        return cur;
    }
}

十六、力扣105. 从前序与中序遍历序列构造二叉树

/**
 * 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 {
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        for(int i = 0; i < inorder.length; i ++){
            map.put(inorder[i],i);
        }
        return fun(inorder,preorder,0,inorder.length-1,0,preorder.length-1);
    }
    public TreeNode fun(int[] inorder,int[] preorder,int inStart,int inEnd,int preStart,int preEnd){
        if(inStart > inEnd){
            return null;
        }
        int curIdx = map.get(preorder[preStart]);
        TreeNode cur = new TreeNode(preorder[preStart]);
        cur.left = fun(inorder,preorder,inStart,curIdx-1,preStart+1,preStart+curIdx-inStart);
        cur.right = fun(inorder,preorder,curIdx+1,inEnd,preEnd-(inEnd-curIdx)+1,preEnd);
        return cur;
    }
}

十七、力扣654. 最大二叉树

/**
 * 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 constructMaximumBinaryTree(int[] nums) {
        return fun(nums, 0, nums.length-1);
    }
    public TreeNode fun(int[] nums, int l, int r){
        if(l > r){
            return null;
        }
        int idx = max(nums, l, r);
        TreeNode cur = new TreeNode(nums[idx]);
        cur.left = fun(nums, l, idx-1);
        cur.right = fun(nums, idx+1, r);
        return cur;
    }
    public int max(int[] nums, int l, int r){
        int res = Integer.MIN_VALUE;
        int idx = 0;
        for(int i = l; i <= r; i ++){
            if(nums[i] > res){
                idx = i;
            }
            res = Math.max(res, nums[i]);
        }
        return idx;
    }
}

十八、力扣617. 合并二叉树

/**
 * 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 mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null && root2 == null){
            return null;
        }
        if(root1 == null && root2 != null){
            return root2;
        }
        if(root1 != null && root2 == null){
            return root1;
        }
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }
}

十九、力扣700. 二叉搜索树中的搜索

/**
 * 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 searchBST(TreeNode root, int val) {
        if(root == null){
            return null;
        }
        if(val == root.val){
            return root;
        }else if(val > root.val){
            return searchBST(root.right, val);
        }else{
            return searchBST(root.left, val);
        }
    }
}

二十、力扣98. 验证二叉搜索树

/**
 * 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 {
    List<Integer> list = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        fun(root);
        for(int i = 1; i < list.size(); i ++){
            if(list.get(i) <= list.get(i-1)){
                return false;
            }
        }
        return true;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        fun(root.left);
        list.add(root.val);
        fun(root.right);
    }
}

二十一、力扣530. 二叉搜索树的最小绝对差

/**
 * 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 {
    List<Integer> list = new ArrayList<>();
    public int getMinimumDifference(TreeNode root) {
        fun(root);
        int res = Integer.MAX_VALUE;
        for(int i = 1; i < list.size(); i ++){
            res = Math.min(res, Math.abs(list.get(i) - list.get(i-1)));
        }
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        fun(root.left);
        list.add(root.val);
        fun(root.right);
    }
}

二十二、力扣501. 二叉搜索树中的众数

/**
 * 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 {
    Deque<Integer> deq = new LinkedList<>();
    Map<Integer,Integer> map = new HashMap<>();
    public int[] findMode(TreeNode root) {
        fun(root);
        for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            while(!deq.isEmpty() && map.get(deq.peekLast()) < entry.getValue()){
                    deq.pollLast();
            }
            if(!deq.isEmpty() && entry.getValue() < map.get(deq.peekLast())){
                continue;
            }
            deq.offerLast(entry.getKey());
        }
        int[] res = new int[deq.size()];
        for(int i = 0; i < res.length; i ++){
            res[i] = deq.pollLast();
        }
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        fun(root.left);
        map.put(root.val, map.getOrDefault(root.val,0)+1);
        fun(root.right);
    }
}

二十三、力扣236. 二叉树的最近公共祖先

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return fun(root,p,q);
    }
    public TreeNode fun(TreeNode root, TreeNode p, TreeNode q){
        if(root == null){
            return null;
        }
        TreeNode l = fun(root.left, p, q);
        TreeNode r = fun(root.right,p,q);
        if(root == p || root == q){
            if(l != null || r != null){
                return root;
            }
            return root;
        }
        if(l != null && r != null){
            return root;
        }
        if(l != null){
            return l;
        }
        if(r != null){
            return r;
        }
        return null;
    }
}

二十四、力扣235. 二叉搜索树的最近公共祖先

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        TreeNode l = lowestCommonAncestor(root.left,p,q);
        TreeNode r = lowestCommonAncestor(root.right,p,q);
        if(root == p || root == q){
            if(l != null || r != null){
                return root;
            }
            return root;
        }
        if(l != null && r != null){
            return root;
        }
        if(l != null){
            return l;
        }
        if(r != null){
            return r;
        }
        return null;
    }
}

二十五、力扣701. 二叉搜索树中的插入操作

/**
 * 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 insertIntoBST(TreeNode root, int val) {
        if(root == null){
            root = new TreeNode(val);
            return root;
        }
        if(root.val > val){
            if(root.left == null){
                root.left = new TreeNode(val);
                return root;
            }
            root.left = insertIntoBST(root.left,val);
        }else{
            if(root.right == null){
                root.right = new TreeNode(val);
                return root;
            }
            root.right = insertIntoBST(root.right,val);
        }
        return root;
    }
}

二十六、力扣450. 删除二叉搜索树中的节点

/**
 * 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 deleteNode(TreeNode root, int key) {
        if(root == null){
            return root;
        }
        if(key == root.val){
            if(root.left == null && root.right == null){
                return null;
            }else if(root.left == null && root.right != null){
                return root.right;
            }else if(root.left != null && root.right == null){
                return root.left;
            }else{
                TreeNode t1 = root.left;
                while(t1.right != null){
                    t1 = t1.right;
                }
                t1.right = root.right.left;
                root.right.left = root.left;
                return root.right;
            }
        }else if(key > root.val){
            root.right = deleteNode(root.right,key);
        }else{
            root.left = deleteNode(root.left,key);
        }
        return root;
    }
}

二十七、力扣669. 修剪二叉搜索树

/**
 * 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 root;
        }
        if(root.val < low){
            return trimBST(root.right,low,high);
        }else if(root.val > high){
            return trimBST(root.left,low,high);
        }else{
            root.left = trimBST(root.left,low,high);
            root.right = trimBST(root.right,low,high);
        }
        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) {
        return fun(nums,0,nums.length-1);
    }
    public TreeNode fun(int[] nums, int low, int high){
        if(low > high){
            return null;
        }
        int cur = (low+high)/2;
        TreeNode root = new TreeNode(nums[cur]);
        root.left = fun(nums,low,cur-1);
        root.right = fun(nums,cur+1, high);
        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 count = 0;
    public TreeNode convertBST(TreeNode root) {
        if(root == null){
            return null;
        }
        root.right = convertBST(root.right);
        root.val += count;
        count = root.val;
        root.left = convertBST(root.left);
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值