538. Convert BST to Greater Tree

题目描述

在这里插入图片描述

方法思路

Approach1: recursive + inorder traversal

class Solution {
    //Runtime: 10 ms, faster than 28.05%
    //Memory Usage: 39.6 MB, less than 71.62%
    Stack<TreeNode> stk;
    public TreeNode convertBST(TreeNode root) {
        stk = new Stack<>();
        int sum = 0;
        inorder(root);
        while(!stk.isEmpty()){
            stk.peek().val += sum;
            sum = stk.pop().val;
        }
        return root;
    }
    public void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        stk.push(root);
        inorder(root.right);
    }
}

Approach2: 上述方法的优化

class Solution {
    //Runtime: 5 ms, faster than 100.00%
    //Memory Usage: 39.7 MB, less than 69.82%
    private int sum = 0;
    public TreeNode convertBST(TreeNode root) {
        if (root != null) {
            convertBST(root.right);
            sum += root.val;
            root.val = sum;
            convertBST(root.left);
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is one possible implementation of the `to_bst` function in Python: ```python def to_bst(lst): # Sort the input list in ascending order lst.sort() n = len(lst) # Initialize an array of size n with all elements set to None tree = [None] * n # Recursive helper function to build the BST def build_tree(start, end, parent): if start > end: return # Find the middle element and set it as the parent of the current subtree mid = (start + end) // 2 tree[mid] = parent # Recursively build the left and right subtrees build_tree(start, mid - 1, mid) build_tree(mid + 1, end, mid) # Build the tree starting from the root build_tree(0, n - 1, None) return tree ``` This function first sorts the input list in ascending order. It then initializes an array of size `n` with all elements set to `None`, where `n` is the length of the input list. It then defines a recursive helper function `build_tree` to build the BST. The `build_tree` function takes as input the start and end indices of the current subtree and the parent index of the current node in the tree. It first checks if the start index is greater than the end index, in which case it returns without doing anything. Otherwise, it finds the middle element between the start and end indices and sets it as the parent of the current subtree in the `tree` array. It then recursively builds the left and right subtrees by calling `build_tree` with the appropriate start and end indices and the current node index as the parent index. Finally, the `to_bst` function calls `build_tree` with the root node index (`0` for a complete binary tree) and `None` as the parent index to build the entire BST. It returns the resulting `tree` array. Here's how you can use this function to convert the input list `[29, 72, 1, 34, 22]` to a complete BST: ```python keys = [29, 72, 1, 34, 22] tree = to_bst(keys) print(tree) # [34, 22, 72, 1, 29] ``` The output should be `[34, 22, 72, 1, 29]`, which is the array representation of the complete BST.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值