[LeetCode] Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

Input:
    1
   /   0   2

  L = 1
  R = 2

Output:
    1
             2

Example 2:

Input:
    3
   /   0   4
       2
   /
  1

  L = 1
  R = 3

Output:
      3
     /
   2
  /
 1

修剪一个二叉树:给定一个二叉树的最大和最小边界L和R,使树中的元素位于L和R之间。

1. 当root位于L和R之间时,递归的修剪其左右子树,并返回root。

2. 当root的值小于L,其左子树都小于L,故舍弃其左子树,递归的修剪其右子树,并返回修剪过的右子树。

3. 当root的值大于R,其右子树都大于R,故舍弃其右子树,递归的修剪其左子树,并返回修剪过的左子树。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if (root == nullptr)
            return 0;
        if (root->val < L)
            return trimBST(root->right, L, R);
        else if (root->val > R)
            return trimBST(root->left, L, R);
        else {
            root->left = trimBST(root->left, L, R);
            root->right = trimBST(root->right, L, R);
            return root;
        }
    }
};
// 16ms
时间: 2024-07-30 10:17:39

[LeetCode] Trim a Binary Search Tree的相关文章

[LeetCode] Trim a Binary Search Tree 修剪一棵二叉树

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary

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

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

LeetCode OJ - Validate Binary Search Tree

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

leetcode 之 Recover Binary Search Tree

Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? c

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

1.  Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solut

[Leetcode][BST][Validate Binary Search Tree]

判断一颗树是不是二分查找树,非常经典基础的一个算法. 我很久之前第一次做的时候,是先求出来了树的前序遍历的结果,然后判断这个数组排序后是否和排序前相同,还要判断重复虾米的,很纠结的一种做法. 后来思考了一下怎么用递归的思路做,觉得应该根据定义返回两个子树的最大值和最小值,写了一会代码,发现好麻烦,不太对的样子. 后来看了题解,发现是用了一种反向的思维,把上下界从树的顶端传下去,而不是自下而上的约束.作者太机智了. 1 /** 2 * Definition for binary tree 3 *

[LeetCode 题解]: Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys