leetcode算法:Trim a Binar 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      \       2Example 2:Input:     3   / \  0   4   \    2   /  1

L = 1  R = 3

Output:       3     /    2     / 1

这道题描述的需求是:给我们一个二叉排序树 最小数l 和最大数r我们需要做的是对这棵树进行裁剪,让树里所有的节点值都满足在l和r之间

思想:二叉排序树的特点是:对于任意一个节点,左子树任意节点数值都比跟节点小,右子树任意节点数值都比根大所以考虑,对于任意一个节点,如果值比l还小,那就应该抛弃这个根,去右子树寻找新的根如果值比r还大,那就应该抛弃这个根,去左子树寻找新的根

这样的操作进行递归就可以了

我的python代码:
 1 # Definition for a binary tree node.
 2 class TreeNode:
 3     def __init__(self, x):
 4         self.val = x
 5         self.left = None
 6         self.right = None
 7
 8 class Solution:
 9     def trimBST(self, root, L, R):
10         """
11         :type root: TreeNode
12         :type L: int
13         :type R: int
14         :rtype: TreeNode
15         """
16         if root is None:
17             return None
18         if root.val >R:
19             return self.trimBST(root.left ,L,R)
20         if root.val<L:
21             return self.trimBST(root.right, L, R)
22         root.left = self.trimBST(root.left,L,R)
23         root.right = self.trimBST(root.right,L,R)
24         return root
25
26
27
28 if __name__ == ‘__main__‘:
29     s = Solution()
30
31     root = TreeNode(1)
32     root.left = TreeNode(0)
33     root.right = TreeNode(2)
34
35     print(root.val,root.left.val,root.right.val)
36
37     root = s.trimBST(root,1,2)
38
39     print(root, root.left, root.right)
				
时间: 2024-10-27 11:07:59

leetcode算法:Trim a Binar Search Tree的相关文章

[leetcode]Tree-669. 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寻找范围内的二叉搜索树

根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 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); TreeNode res = new TreeNode(ro

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详细分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

leetcode算法: Find Bottom Left Tree Value

leetcode算法: Find Bottom Left Tree Value Given a binary tree, find the leftmost value in the last row of the tree. Example 1:Input: 2 / \ 1 3 Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output:7Note: You may assume the tree (i.e., the given ro

leetcode第一刷_Recover Binary Search Tree

这是一道好题,思路虽然有,但是提交之后总是有数据过不了,又按照数据改改改,最后代码都没法看了.收到的教训是如果必须为自己的代码加上很多很多特殊的限定,来过一些特殊的数据的话,说明代码本身有很大的漏洞. 这道题,我想到了要用两个指针保存乱序的节点,甚至想到了用一个pre指针来保存前面一个节点,但是问题出在哪里呢?我觉得应该是自己对树的遍历理解的不够深刻.既然知道了二叉搜索树一定是用中序遍历的,那么程序的框架应该马上写的出来,先左子树,再根,再右子树,那你说什么时候更新pre指针呢,当然是访问根节点

[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] 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

669. 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