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;
    };

     

  • 相关阅读:
    微服务网关常用限流算法
    微服务网关zuul介绍
    Nginx实现微服务网关的简单介绍
    11.9-编写操作者
    11.5-编写程序
    11.3-学习操作者文档
    11.2-LV面向对象编程视频学习及周五与老师交流总结
    10.29-基于LabVIEW的分布式集群机器人控制系统
    10.27-运用操作者框架架设控制中心软件架构
    5.24-29离线解析问题
  • 原文地址:https://www.cnblogs.com/wltree/p/15705154.html
Copyright © 2011-2022 走看看