其他分享
首页 > 其他分享> > LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

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;
    }
}

参考资料

标签:Trim,Binary,right,TreeNode,669,int,trimBST,return,root
来源: https://www.cnblogs.com/hglibin/p/10994693.html