[LeetCode] 669. Trim a Binary Search Tree

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.

Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.

Example 1:

Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]

Example 2:

Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]

Example 3:

Input: root = [1], low = 1, high = 2
Output: [1]

Example 4:

Input: root = [1,null,2], low = 1, high = 3
Output: [1,null,2]

Example 5:

Input: root = [1,null,2], low = 2, high = 4
Output: [2]

Constraints:

  • The number of nodes in the tree in the range [1, 104].
  • 0 <= Node.val <= 104
  • The value of each node in the tree is unique.
  • root is guaranteed to be a valid binary search tree.
  • 0 <= low <= high <= 104

修剪二叉搜索树。

给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树不应该改变保留在树中的元素的相对结构(即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在唯一的答案。

所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/trim-a-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是递归。题意是给了low和high两个边界,请你去掉BST中节点值不在这个范围内的节点但是保持其他节点的相对关系。思路大概是如下几点

如果root为空,就返回root

如果root不为空且root.val < low则去看右子树

如果root不为空且root.val > high则去看左子树

递归地去检查root的左孩子和右孩子

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public TreeNode trimBST(TreeNode root, int low, int high) {
18         // corner case
19         if (root == null) {
20             return null;
21         }
22         if (root.val < low) {
23             return trimBST(root.right, low, high);
24         }
25         if (root.val > high) {
26             return trimBST(root.left, low, high);
27         }
28         root.left = trimBST(root.left, low, high);
29         root.right = trimBST(root.right, low, high);
30         return root;
31     }
32 }

 

JavaScript实现

 1 /**
 2  * Definition for a binary tree node.
 3  * function TreeNode(val, left, right) {
 4  *     this.val = (val===undefined ? 0 : val)
 5  *     this.left = (left===undefined ? null : left)
 6  *     this.right = (right===undefined ? null : right)
 7  * }
 8  */
 9 /**
10  * @param {TreeNode} root
11  * @param {number} low
12  * @param {number} high
13  * @return {TreeNode}
14  */
15 var trimBST = function (root, low, high) {
16     // corner case
17     if (root === null) {
18         return null;
19     }
20     if (root.val < low) {
21         return trimBST(root.right, low, high);
22     }
23     if (root.val > high) {
24         return trimBST(root.left, low, high);
25     }
26     root.left = trimBST(root.left, low, high);
27     root.right = trimBST(root.right, low, high);
28     return root;
29 };

 

LeetCode 题目总结

posted @ 2021-02-03 16:14  CNoodle  阅读(159)  评论(0编辑  收藏  举报