Convert BST to Greater Tree

题目:

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:
              5
            /   \
           2     13

Output: The root of a Greater Tree like this:
             18
            /   \
          20    13

题目意思:

给一个二叉搜索树, 将它转化为更大的二叉树,使得节点的值为原始节点的值加上所有比它大的节点的值之和,并且使转化后的二叉树的左子树大于右子树。

解题思路:

设置一个数组,对二叉树进行中序遍历,用数组保存这些值,得到从小到大的一个数组;再次对二叉树进行中序遍历,将所遍历节点的值加上所有比它大的节点的值;对数组进行反转,即让它的值从大到小排序,然后对每个节点求和后将数组最后一个元素丢掉,因为最大的那个值不用变。

也可以采用一次 bst 遍历,然后进行递归调用。


代码如下:

class Solution {
 public:
 int s;
 void  work(TreeNode* root) {
     if(!root) return;
     if(!root->left && !root->right) {
         root->val += s;
         s = root->val;
         return;
     }
     if(root->right) work(root->right);
     root->val += s;
     s = root->val;
     work(root->left);
 }
 TreeNode* convertBST(TreeNode* root) {
     s = 0;
     work(root);
     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、付费专栏及课程。

余额充值