从二叉搜索树到更大和树 : BST 中序遍历的经典运用

题目描述

这是 LeetCode 上的 1038. 从二叉搜索树到更大和树 ,难度为 中等

Tag : 「BST」、「中序遍历」

给定一个二叉搜索树 root (BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。

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

  • 节点的左子树仅包含键小于节点键的节点。
  • 节点的右子树仅包含键大于节点键的节点。
  • 左右子树也必须是二叉搜索树。

示例 1:

csharp 复制代码
输入:[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:

ini 复制代码
输入:root = [0,null,1]

输出:[1,null,1]

提示:

  • 树中的节点数在 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ 1 , 100 ] [1, 100] </math>[1,100] 范围内。
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 < = N o d e . v a l < = 100 0 <= Node.val <= 100 </math>0<=Node.val<=100
  • 树中的所有值均不重复 。

中序遍历

利用 BST 的中序遍历是有序 的特性,我们可以通过两次遍历 BST 来求解问题。

首先,通过一次遍历,计算出整棵树的节点总和 tot,然后在中序遍历过程中,不断对 tot 进行更新,将其作为当前未遍历到的节点的总和,用于给当前节点赋值。

假设当前遍历到的节点为 x(起始节点值为 t),那么将节点更新为当前节点 tot 后,更新 tot = tot - t

这是常规的中序遍历做法,更进一步,如果将其中序遍历的顺序进行翻转(从「左中右」调整为「右中左」),则可实现一次遍历。

Java 代码:

Java 复制代码
class Solution {
    int tot = 0;
    public TreeNode bstToGst(TreeNode root) {
        dfs(root);
        return root;
    }
    void dfs(TreeNode root) {
        if (root == null) return ;
        dfs(root.right);
        tot += root.val;
        root.val = tot;
        dfs(root.left);
    }
}

C++ 代码:

C++ 复制代码
class Solution {
public:
    int tot = 0;
    TreeNode* bstToGst(TreeNode* root) {
        dfs(root);
        return root;
    }
    void dfs(TreeNode* root) {
        if (root == nullptr) return;
        dfs(root->right);
        tot += root->val;
        root->val = tot;
        dfs(root->left);
    }
};

Python 代码:

Python 复制代码
class Solution:
    def bstToGst(self, root: TreeNode) -> TreeNode:
        tot = 0
        def dfs(root):
            nonlocal tot
            if not root: return
            dfs(root.right)
            tot += root.val
            root.val = tot
            dfs(root.left)
        dfs(root)
        return root

TypeScript 代码:

TypeScript 复制代码
function bstToGst(root: TreeNode | null): TreeNode | null {
    let tot = 0;
    const dfs = function(root: TreeNode | null): void {
        if (!root) return ;
        dfs(root.right);
        tot += root.val;
        root.val = tot;
        dfs(root.left);
    }
    dfs(root);
    return root;
};
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.1038 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour...

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
blackorbird1 天前
Edge 浏览器 IE 模式成攻击突破口:黑客借仿冒网站诱导攻击
前端·edge
uzong1 天前
一次慢接口背后,竟藏着40+种可能!你中过几个
后端·面试·程序员
G探险者1 天前
滴滴P0级故障背后:互联网公司是如何分级处理线上事故的?
后端
谷歌开发者1 天前
Web 开发指向标 | Chrome 开发者工具学习资源 (一)
前端·chrome·学习
名字越长技术越强1 天前
Chrome和IE获取本机ip地址
前端
天***88961 天前
Chrome 安装失败且提示“无可用的更新” 或 “与服务器的连接意外终止”,Chrome 离线版下载安装教程
前端·chrome
半梦半醒*1 天前
zabbix安装
linux·运维·前端·网络·zabbix
G探险者1 天前
从 Tomcat 与 Jetty 的对比,聊聊影响一个服务并发能力的关键因素
后端
清羽_ls1 天前
React Hooks 核心规则&自定义 Hooks
前端·react.js·hooks
你的人类朋友1 天前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全