zoukankan      html  css  js  c++  java
  • 538. 把二叉搜索树转换为累加树

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

    题目链接:538. 把二叉搜索树转换为累加树(中等)

    给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。

    提醒一下,二叉搜索树满足下列约束条件:

    • 节点的左子树仅包含键 小于 节点键的节点。

    • 节点的右子树仅包含键 大于 节点键的节点。

    • 左右子树也必须是二叉搜索树。

    示例 1:

    输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
    输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

    示例 2:

    输入:root = [0,null,1]
    输出:[1,null,1]

    示例 3:

    输入:root = [1,0,2]
    输出:[3,3,2]

    示例 4:

    输入:root = [3,2,4,1]
    输出:[7,9,4,10]

    提示:

    • 树中的节点数介于 0104 之间。

    • 每个节点的值介于 -104104 之间。

    • 树中的所有值 互不相同

    • 给定的树为二叉搜索树。

    解题思路

    本题给的是二叉搜索树,中序遍历是有序的。从题目的要求中,可以发现只需要采取右中左顺序进行遍历,在遍历的同时累加每一个节点的值作为当前节点的值即可。

    递归

    C++

    //递归法(右中左)
    class Solution {
    public:
        void traversal(TreeNode* node, int& sum) {
            if (node == nullptr) return;
            traversal(node->right, sum); //
            sum += node->val; //
            node->val = sum;
            traversal(node->left, sum); //
        }
        TreeNode* convertBST(TreeNode* root) {
            int sum = 0; //累加
            traversal(root, sum);
            return root;
        }
    };

    JavaScript

    /**
     * 递归
     * @param {TreeNode} root
     * @return {TreeNode}
     */
    var convertBST = function(root) {
        let sum = 0;
        const traversal = (node) => {
            if (node === null) return;
            traversal(node.right);
            sum += node.val;
            node.val = sum;
            traversal(node.left);
        }
        traversal(root, sum);
        return root;
    };

    迭代

    C++

    //迭代法(右中左)
    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            TreeNode* cur = root;
            stack<TreeNode*> staNode;
            int sum = 0;
            while (cur != nullptr || !staNode.empty()) {
                if (cur != nullptr) {
                    staNode.push(cur);
                    cur = cur->right; //
                } else {
                    cur = staNode.top(); //
                    staNode.pop();
                    sum += cur->val;
                    cur->val = sum;
                    cur = cur->left; //
                }
            }
            return root;
        }
    };

    JavaScript

    /**
     * 迭代
     * @param {TreeNode} root
     * @return {TreeNode}
     */
    var convertBST = function(root) {
        let cur = root;
        let staNode = [];
        let sum = 0;
        while (cur != null || staNode.length != 0) {
            if (cur != null) {
                staNode.push(cur);
                cur = cur.right;
            } else {
                cur = staNode.pop();
                sum += cur.val;
                cur.val = sum;
                cur = cur.left;
            }
        }
        return root;
    };

    统一迭代

    C++

    //统一迭代法(右中左=》左中null右)
    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            stack<TreeNode*> staNode;
            if (root != nullptr) staNode.push(root);
            int sum = 0;
            while (!staNode.empty()) {
                TreeNode* node = staNode.top();
                if (node != nullptr) {
                    staNode.pop();
                    if (node->left) staNode.push(node->left); //
                    staNode.push(node); //
                    staNode.push(nullptr); // null
                    if (node->right) staNode.push(node->right); //
                } else {
                    staNode.pop();
                    node = staNode.top();
                    staNode.pop();
                    sum += node->val;
                    node->val = sum;
                }
            }
            return root;
        }
    };

    JavaScript

    /**
     * 统一迭代
     * @param {TreeNode} root
     * @return {TreeNode}
     */
    var convertBST = function(root) {
        let staNode = [];
        let sum = 0;
        if (root != null) staNode.push(root);
        while (staNode.length != 0) {
            let node = staNode.pop();
            if (node != null) {
                if (node.left) staNode.push(node.left);
                staNode.push(node);
                staNode.push(null);
                if (node.right) staNode.push(node.right);
            } else {
                node = staNode.pop();
                sum += node.val;
                node.val = sum;
            }
        }
        return root;
    };

     

  • 相关阅读:
    C# asp:Repeater DataSource List<T>
    MySQL DATE_FORMATE函数内置字符集的坑_转小叶子爹
    MySQL count(distinct) 逻辑的一个bug
    org.hibernate.PersistentObjectException: detached entity passed to persist:
    CGLIB Enhancement failed
    firstResult/maxResults specified on polymorphic query;
    Last packet sent to the server was 0 ms ago.
    MySql Error Code: 2006 – MySQl
    InnoDB: Error: auto-extending data file ./ibdata1 is of a different size
    mysql 大数据量分页处理
  • 原文地址:https://www.cnblogs.com/wltree/p/15705154.html
Copyright © 2011-2022 走看看