二叉树专题

Base on : 代码随想录

二叉树的属性

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) {
        // 其实就是左右节点交换
        if(root == null) return root;
        invertTree(root.left);
        invertTree(root.right);
        // 交换左右节点
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }

    
}
/**
 * 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) {
        // 迭代, 层序遍历, 交换最右子节点
        if(root == null) return root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            while(len-- > 0){
                TreeNode node = queue.poll();
                // 交换左右子节点
                swap(node);
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
        }
        return root;
    }
    private void swap(TreeNode node){
        TreeNode temp = node.left;
        node.left = node.right;
        node.right = temp;
    }
}

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) {
        return combain(root.left, root.right);
    }

    private boolean combain(TreeNode left, TreeNode right){
        if(left == null && right == null) return true;
        if(left == null || right == null) return false;
        if(left.val != right.val) return false;
        return combain(left.left, right.right) && combain(left.right, right.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 boolean isSymmetric(TreeNode root) {
        // 迭代
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);

        while(!queue.isEmpty()){
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();
            // 左右节点为空了
            if(left == null && right == null) continue;

            if(left == null || right == null || left.val != right.val) return false;


            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }
        return true;
    }
}

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 {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 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 {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if(root.left == null) return 1 + rightDepth;
        if(root.right == null) return 1 + leftDepth;
        // 叶子结点
        return 1 + Math.min(leftDepth, rightDepth);
    }
}

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

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;
        if(Math.abs(getMaxDepth(root.left) - getMaxDepth(root.right)) > 1) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }

    private int getMaxDepth(TreeNode node){
        if(node == null) return 0;
        return 1 + Math.max(getMaxDepth(node.left), getMaxDepth(node.right));
    }
}

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> ans = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    
    public List<String> binaryTreePaths(TreeNode root) {
        visit(root);
        return ans;
    }
    private void visit(TreeNode root){
        path.add(root.val);
        // 叶子结点
        if(root.left == null && root.right == null){
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < path.size() - 1; i++){
                sb.append(path.get(i)).append("->");
            }
            sb.append(path.get(path.size()-1));
            ans.add(sb.toString());
            return;
        }
        if(root.left != null){
            visit(root.left);
            path.removeLast();
        }
        if(root.right != null){
            visit(root.right);
            path.removeLast();
        }
    }
}

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 {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) return 0;
        int left = sumOfLeftLeaves(root.left);
        int right = sumOfLeftLeaves(root.right);
        int ans = 0;
        if(root.left != null && root.left.left == null && root.left.right == null) ans += root.left.val;
        return ans + left + 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 ans = -1, maxDep = -1;
    public int findBottomLeftValue(TreeNode root) {
        visit(root, 0);
        return ans;
    }
    // 前序遍历
    // 中左右
    private void visit(TreeNode root, int curDep){
        if(root == null) return;

        if(curDep > maxDep){
            ans = root.val;
            maxDep = curDep;
        }


        visit(root.left, curDep + 1);
        visit(root.right, curDep + 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) {
        if(root == null) return false;
        return visit(root, targetSum - root.val);
    }
    private boolean visit(TreeNode root, int cur){
        if(root.left == null && root.right == null){
            if(cur == 0) return true;
            else return false;
        }
        if(root.left != null && visit(root. left, cur - root.left.val)) return true;
        if(root.right != null && visit(root. right, cur - root.right.val)) return true;
        return false;
    }
}
/**
 * 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) {
        if(root == null) return false;
        // 先序遍历
        targetSum -= root.val;
        if(root.left == null && root.right == null && targetSum == 0) return true;
        // 左
        if(root.left != null && hasPathSum(root.left,targetSum)) return true;
        // 右
        if(root.right != null && hasPathSum(root.right,targetSum))  return true;
        return false;
    }
}

在这里插入图片描述

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>> ans = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null) return ans;
        visit(root, targetSum - root.val);
        return ans;
    }
    private void visit(TreeNode node, int cur){
        path.add(node.val);
        if(node.left == null && node.right == null){
            if(cur == 0) ans.add(new ArrayList(path));
            return;
        }
        if(node.left != null){
            visit(node.left, cur - node.left.val);
            path.removeLast();
        }
        if(node.right != null){
            visit(node.right, cur - node.right.val);
            path.removeLast();
        }
    }
}

在这里插入图片描述

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 {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return build(inorder, 0, inorder.length, postorder, 0, postorder.length);
    }

    // 左闭右开
    private TreeNode build(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight){
        // 没有节点
        if(inLeft >= inRight) return null;
        // 还剩一个节点
        if(inRight - inLeft == 1) return new TreeNode(inorder[inLeft]);
        // 后序遍历找到中间的节点
        int rootVal = postorder[postRight - 1];
        TreeNode root = new TreeNode(rootVal);
        // 通过中序遍历找到中间节点
        int rootIdx = -1;
        for(int i = inLeft; i < inRight; i++){
            if(inorder[i] == rootVal){
                rootIdx = i;
                break;
            }
        }
        // 左右可以开始划分了
        root.left = build(inorder, inLeft, rootIdx, postorder, postLeft, postLeft + (rootIdx - inLeft));
        root.right = build(inorder, rootIdx + 1, inRight, postorder, postLeft + (rootIdx - inLeft), postRight - 1);
        return root;
    }
}

在这里插入图片描述

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 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return build(preorder, 0, preorder.length, inorder, 0, inorder.length);
    }

    private TreeNode build(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight){
        // 无节点了
        if(preLeft >= preRight) return null;
        // 还剩一个节点
        if(preRight - preLeft == 1) return new TreeNode(preorder[preLeft]);
        // 根节点
        int rootVal = preorder[preLeft];
        TreeNode root = new TreeNode(rootVal);
        int rootIdx = -1;
        for(int i = inLeft; i < inRight; i++){
            if(inorder[i] == rootVal){
                rootIdx = i;
                break;
            }
        }
        root.left = build(preorder, preLeft + 1, preLeft + (rootIdx - inLeft) + 1 , inorder, inLeft, rootIdx);
        root.right = build(preorder, preLeft + (rootIdx - inLeft) + 1, preRight, inorder, rootIdx + 1, inRight);
        return root;
    }
}

在这里插入图片描述

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) {
        if(nums == null || nums.length == 0) return null;
        return build(nums, 0, nums.length);
    }
    // 左闭右开
    private TreeNode build(int[] nums, int left, int right){
        if(left >= right) return null;
        if(right - left == 1) return new TreeNode(nums[left]);
        int rootValue = nums[left];
        int rootIdx = left;
        for(int i = left; i < right; i++){
            if(nums[i] > rootValue){
                rootValue = nums[i];
                rootIdx = i;
            }
        }
        TreeNode root = new TreeNode(rootValue);
        root.left = build(nums, left, rootIdx);
        root.right = build(nums, rootIdx + 1, right);
        return root;
    }
}

在这里插入图片描述

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

image.png

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> ans = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        visit(root);
        // 遍历list看是否有序
        for(int i = 0; i < ans.size() - 1; i++){
            if(ans.get(i) >= ans.get(i + 1)) return false;
        }
        return true;
    }

    private void visit(TreeNode root){
        if(root == null) return;
        visit(root.left);
        ans.add(root.val);
        visit(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 {
    int ans = Integer.MAX_VALUE;
    TreeNode pre = null;
    public int getMinimumDifference(TreeNode root) {
        // 最小值肯定出现在父子节点之间
        getAns(root);
        return ans;
    }
    // 中序遍历,pre是上个节点,cur是当前节点,有序
    private void getAns(TreeNode cur){
        if(cur == null) return;
        getAns(cur.left);
        if(pre != null){
            if(cur.val - pre.val < ans) ans = cur.val - pre.val;
        }
        // 记录上个节点
        pre = cur;
        getAns(cur.right);        
        
    }
}
/**
 * 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<>();
    int ans = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        visit(root);
        for(int i = 0; i < list.size() - 1; i++){
            if(list.get(i + 1) - list.get(i) < ans) ans = list.get(i + 1) - list.get(i);
        }
        return ans;
    }

    private void visit(TreeNode root){
        if(root == null) return;
        visit(root.left);
        list.add(root.val);
        visit(root.right);
    }
}

image.png

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 {
    // 当做普通二叉树做
    int max = -1, count = 0;
    Map<Integer, Integer> map = new HashMap<>();
    public int[] findMode(TreeNode root) {
        visit(root);
        int[] ans = new int[count];
        for(int key : map.keySet()){
            if(map.get(key) == max) ans[--count] = key;
        }
        return ans;
    }

    private void visit(TreeNode root){
        if(root == null) return;
        visit(root.left);
        visit(root.right);
        map.put(root.val, map.getOrDefault(root.val, 0) + 1);
        if(map.get(root.val) > max) {
            count = 1;
            max = map.get(root.val);
        }else if(map.get(root.val) == max) count++;
    }
}
/**
 * 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 {
    TreeNode pre = null;
    int count = 0, maxNum = 0;
    List<Integer> list = new ArrayList<>();
    public int[] findMode(TreeNode root) {
        visit(root);
        int[] ans = new int[list.size()];
        for(int i = 0; i < list.size(); i++) ans[i] = list.get(i);
        return ans;
    }
    // 中序遍历
    private void visit(TreeNode cur){
        if(cur == null) return;
        visit(cur.left);
        int curVal = cur.val;
        if(pre == null || pre.val != curVal) count = 0;
        else count++;
        if(count > maxNum){
            maxNum = count;
            // 清理
            list.clear();
            list.add(curVal);
        }else if(count == maxNum) list.add(curVal);
        pre = cur;
        visit(cur.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) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null && right == null) return null;
        else if(left == null) return right;
        else if(right == null) return left;
        return root;
    }
}

在这里插入图片描述

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) {
        // 如果 cur 在 [p, q] 之间就是最近公共祖先了
        if(root == null || root == p || root == q) return root;
        // 往左找
        if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
        // 往右找
        if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
        return root;
        
    }
}

在这里插入图片描述

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){
            TreeNode node = new TreeNode(val);
            return node;
        }
        if(root.val < val) root.right = insertIntoBST(root.right, val);
        if(root.val > val) root.left = insertIntoBST(root.left, 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(root.val == key){
            if(root.left == null) return root.right;
            if(root.right == null) return root.left;
            // 左右节点都存在
            TreeNode cur = root.right;
            while(cur.left != null) cur = cur.left;
            cur.left = root.left;
            return root.right;
        }
        if(root.val > key) root.left = deleteNode(root.left, key);
        if(root.val < key) root.right = deleteNode(root.right, 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 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. 将有序数组转换为二叉搜索树

/**
 * 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 build(nums, 0, nums.length);
    }

    private TreeNode build(int[] nums, int left, int right){
        if(left >= right) return null;
        if(right - left == 1) return new TreeNode(nums[left]);
        int mid = (left + right) >> 1;
        int rootVal = nums[mid];
        TreeNode root = new TreeNode(rootVal);
        root.left = build(nums, left, mid);
        root.right = build(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) {
        visit(root);
        return root;
    }
    // 反中序遍历
    private void visit(TreeNode root){
        if(root == null) return;
        visit(root.right);          // 右
        sum += root.val;
        root.val = sum;             // 中
        visit(root.left);           // 左
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值