LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

669. 修剪二叉搜索树
669. Trim a Binary Search Tree

题目描述

LeetCode

LeetCode669. Trim a Binary Search Tree简单

Java 实现
TreeNode Class

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
class Solution {
    public TreeNode trimBST(TreeNode root, int L, int R) {
        if (root == null) {
            return null;
        }
        if (root.val < L) {
            return trimBST(root.right, L, R);
        }
        if (root.val > R) {
            return trimBST(root.left, L, R);
        }
        root.left = trimBST(root.left, L, R);
        root.right = trimBST(root.right, L, R);
        return root;
    }
}

参考资料

上一篇:"页内标签"组件: —— 快应用组件库H-UI


下一篇:LeetCode699 修剪二叉搜索树