669. Trim a Binary Search Tree

桃扇骨 2022-06-03 02:14 252阅读 0赞

原题链接

  1. /** * Created by Joe on 2017/12/14. * https://leetcode.com/problems/trim-a-binary-search-tree/description/ */
  2. public class P669 {
  3. public TreeNode trimBST(TreeNode root, int L, int R) {
  4. if (root == null) return null;
  5. if (root.val < L) return trimBST(root.right, L, R);
  6. if (root.val > R) return trimBST(root.left, L, R);
  7. root.left = trimBST(root.left, L, R);
  8. root.right = trimBST(root.right, L, R);
  9. return root;
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读