538 Convert BST to Greater Tree 把二叉搜索树转换为累加树

给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。
例如:
输入: 二叉搜索树:
              5
            /   \
           2     13
输出: 转换为累加树:
             18
            /   \
          20     13
详见:https://leetcode.com/problems/convert-bst-to-greater-tree/description/

C++:

方法一:

/**
 * 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* convertBST(TreeNode* root)
    {
        int sum = 0;
        helper(root, sum);
        return root;
    }
    void helper(TreeNode*& node, int& sum)
    {
        if (!node)
        {
            return;
        }
        helper(node->right, sum);
        node->val += sum;
        sum = node->val;
        helper(node->left, sum);
    }
};

方法二:

/**
 * 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* convertBST(TreeNode* root) {
        if (!root)
        {
            return nullptr;
        }
        int sum = 0;
        stack<TreeNode*> stk;
        TreeNode *p = root;
        while (p || !stk.empty())
        {
            while (p)
            {
                stk.push(p);
                p = p->right;
            }
            p = stk.top();
            stk.pop();
            p->val += sum;
            sum = p->val;
            p = p->left;
        }
        return root;
    }
};

参考:http://www.cnblogs.com/grandyang/p/6591526.html

原文地址:https://www.cnblogs.com/xidian2014/p/8910068.html

时间: 2024-09-30 15:21:42

538 Convert BST to Greater Tree 把二叉搜索树转换为累加树的相关文章

538-把二叉搜索树转换为累加树

538-把二叉搜索树转换为累加树 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 著作权归领扣网络所有.商业转载请联系官方授权

LeetCode 把二叉搜索树转换为累加树(538)

第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 概念 二叉搜索树 二叉查找树(Binary Search Tree

leetcode 538. 把二叉搜索树转换为累加树

题目 C++代码 /** * 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* convertBST(TreeNode* root) { if(ro

538. 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 thi

538. 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 thi

[LeetCode] 538. 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 thi

[CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原理,请参见我之前的博客Validate Binary Search Tree 验证二叉搜索树.

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

数据结构——二叉搜索树、B树、B-树

数据结构——二叉搜索树.B树.B-树 1. 综述 二叉排序树(Binary Sort Tree),又叫二叉查找树(Binary Search Tree),也叫二叉排序树. 二叉搜索树满足以下性质: 1. 若根节点左子树不为空,则左子树上的所有节点均小于根节点: 2. 若根节点右子树不为空,则右子树上的所有节点均大于根节点: 3. 其左右子树也是二叉搜索树(递归定义): 4. 没有键值相等的点. B树就是B-树.B树/B-树英文叫B-Tree,可能被不小心翻译成了B-树.