zoukankan      html  css  js  c++  java
  • 0669修剪二叉搜索树 Marathon

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

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

    示例 1:

    输入:root = [1,0,2], low = 1, high = 2
    输出:[1,null,2]
    示例 2:

    输入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
    输出:[3,2,null,1]
    示例 3:

    输入:root = [1], low = 1, high = 2
    输出:[1]
    示例 4:

    输入:root = [1,null,2], low = 1, high = 3
    输出:[1,null,2]
    示例 5:

    输入:root = [1,null,2], low = 2, high = 4
    输出:[2]

    提示:

    树中节点数在范围 [1, 104] 内
    0 <= Node.val <= 104
    树中每个节点的值都是唯一的
    题目数据保证输入是一棵有效的二叉搜索树
    0 <= low <= high <= 104

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/trim-a-binary-search-tree

    参考:

    python

    # 0669-修剪二叉搜索树
    
    class Solution1:
        def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
            """
            递归
            :param root:
            :param low:
            :param high:
            :return:
            """
            if not root:
                return None
            # 寻找符合区间的节点 [low, high]
            if root.val < low:
                right = self.trimBST(root.right, low, high)
                return right
            if root.val > high:
                left = self.trimBST(root.left, low, high)
                return left
            # 各自接入符合条件的左右孩子
            root.left = self.trimBST(root.left, low, high)
            root.right = self.trimBST(root.right, low, high)
    
            return root
    
    class Solution2:
        def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
            """
            迭代
            :param root:
            :param low:
            :param high:
            :return:
            """
            if not root:
                return None
            # 处理头结点,root移动到[low, high]范围内
            while root and (root.val < low or root.val > high):
                # 小于low的往右走
                if root.val < low:
                    root = root.right
                # 大于high的往左走
                else:
                    root = root.left
            cur = root
            # root已经在区间内,处理左孩子小于low的情况
            while cur:
                while cur.left and cur.left.val < low:
                    cur.left = cur.left.right
                cur = cur.left
            cur = root
            # root已经在区间内,处理右孩子大于high的情况
            while cur:
                while cur.right and cur.right.val > high:
                    cur.right = cur.right.left
                cur = cur.right
    
            return root
    

    golang

    package binaryTree
    
    // 递归
    func trimBST(root *TreeNode, low int, high int) *TreeNode {
    	if root == nil {
    		return nil
    	}
    	if root.Val < low {
    		right := trimBST(root.Right, low, high)
    		return right
    	}
    	if root.Val > high {
    		left := trimBST(root.Left, low, high)
    		return left
    	}
    
    	root.Left = trimBST(root.Left, low, high)
    	root.Right = trimBST(root.Right, low, high)
    
    	return root
    }
    
    
    // 迭代
    func trimBST2(root *TreeNode, low int, high int) *TreeNode {
    	if root == nil {
    		return nil
    	}
    
    	// 处理root,让root移动到[low, high]
    	for root != nil && (root.Val < low || root.Val > high) {
    		if root.Val < low {
    			root = root.Right
    		} else {
    			root = root.Left
    		}
    	}
    
    	// 此时root已经在区间内,处理左孩子元素小于low
    	cur := root
    	for cur != nil {
    		for cur.Left != nil && cur.Left.Val < low {
    			cur.Left = cur.Left.Right
    		}
    		cur = cur.Left
    	}
    
    	// 此时root已经在区间内,处理右孩子元素大于high
    	cur = root
    	for cur != nil {
    		for cur.Right != nil && cur.Right.Val > high {
    			cur.Right = cur.Right.Left
    		}
    		cur = cur.Right
    	}
    	return root
    }
    
  • 相关阅读:
    Java实现 LeetCode 400 第N个数字
    Java实现 LeetCode 400 第N个数字
    Java实现 LeetCode 399 除法求值
    Java实现 LeetCode 399 除法求值
    Java实现 LeetCode 399 除法求值
    Java实现 LeetCode 398 随机数索引
    Java实现 LeetCode 398 随机数索引
    Java实现 LeetCode 398 随机数索引
    linux中的cd ..和cd -命令有什么区别?
    GCC使用
  • 原文地址:https://www.cnblogs.com/davis12/p/15579822.html
Copyright © 2011-2022 走看看