|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[1038. 从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/solutions/2552959/gong-shui-san-xie-bst-de-zhong-xu-bian-l-vtu1/)** ,难度为 **中等**。 |
| 4 | + |
| 5 | +Tag : 「BST」、「中序遍历」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给定一个二叉搜索树 `root` (BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。 |
| 10 | + |
| 11 | +提醒一下, 二叉搜索树满足下列约束条件: |
| 12 | + |
| 13 | +* 节点的左子树仅包含键小于节点键的节点。 |
| 14 | +* 节点的右子树仅包含键大于节点键的节点。 |
| 15 | +* 左右子树也必须是二叉搜索树。 |
| 16 | + |
| 17 | +示例 1: |
| 18 | + |
| 19 | +``` |
| 20 | +输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] |
| 21 | +
|
| 22 | +输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] |
| 23 | +``` |
| 24 | +示例 2: |
| 25 | +``` |
| 26 | +输入:root = [0,null,1] |
| 27 | +
|
| 28 | +输出:[1,null,1] |
| 29 | +``` |
| 30 | + |
| 31 | +提示: |
| 32 | +* 树中的节点数在 $[1, 100]$ 范围内。 |
| 33 | +* $0 <= Node.val <= 100$ |
| 34 | +* 树中的所有值均不重复 。 |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +### 中序遍历 |
| 39 | + |
| 40 | +利用 **`BST` 的中序遍历是有序** 的特性,我们可以通过两次遍历 `BST` 来求解问题。 |
| 41 | + |
| 42 | +首先,通过一次遍历,计算出整棵树的节点总和 `tot`,然后在中序遍历过程中,不断对 `tot` 进行更新,将其作为当前未遍历到的节点的总和,用于给当前节点赋值。 |
| 43 | + |
| 44 | +假设当前遍历到的节点为 `x`(起始节点值为 `t`),那么将节点更新为当前节点 `tot` 后,更新 `tot = tot - t`。 |
| 45 | + |
| 46 | +这是常规的中序遍历做法,更进一步,如果将其中序遍历的顺序进行翻转(从「左中右」调整为「右中左」),则可实现一次遍历。 |
| 47 | + |
| 48 | + |
| 49 | +Java 代码: |
| 50 | +```Java |
| 51 | +class Solution { |
| 52 | + int tot = 0; |
| 53 | + public TreeNode bstToGst(TreeNode root) { |
| 54 | + dfs(root); |
| 55 | + return root; |
| 56 | + } |
| 57 | + void dfs(TreeNode root) { |
| 58 | + if (root == null) return ; |
| 59 | + dfs(root.right); |
| 60 | + tot += root.val; |
| 61 | + root.val = tot; |
| 62 | + dfs(root.left); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | +C++ 代码: |
| 67 | +```C++ |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + int tot = 0; |
| 71 | + TreeNode* bstToGst(TreeNode* root) { |
| 72 | + dfs(root); |
| 73 | + return root; |
| 74 | + } |
| 75 | + void dfs(TreeNode* root) { |
| 76 | + if (root == nullptr) return; |
| 77 | + dfs(root->right); |
| 78 | + tot += root->val; |
| 79 | + root->val = tot; |
| 80 | + dfs(root->left); |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | +Python 代码: |
| 85 | +```Python |
| 86 | +class Solution: |
| 87 | + def bstToGst(self, root: TreeNode) -> TreeNode: |
| 88 | + tot = 0 |
| 89 | + def dfs(root): |
| 90 | + nonlocal tot |
| 91 | + if not root: return |
| 92 | + dfs(root.right) |
| 93 | + tot += root.val |
| 94 | + root.val = tot |
| 95 | + dfs(root.left) |
| 96 | + dfs(root) |
| 97 | + return root |
| 98 | +``` |
| 99 | +TypeScript 代码: |
| 100 | +```TypeScript |
| 101 | +function bstToGst(root: TreeNode | null): TreeNode | null { |
| 102 | + let tot = 0; |
| 103 | + const dfs = function(root: TreeNode | null): void { |
| 104 | + if (!root) return ; |
| 105 | + dfs(root.right); |
| 106 | + tot += root.val; |
| 107 | + root.val = tot; |
| 108 | + dfs(root.left); |
| 109 | + } |
| 110 | + dfs(root); |
| 111 | + return root; |
| 112 | +}; |
| 113 | +``` |
| 114 | +* 时间复杂度:$O(n)$ |
| 115 | +* 空间复杂度:$O(n)$ |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +### 最后 |
| 120 | + |
| 121 | +这是我们「刷穿 LeetCode」系列文章的第 `No.1038` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 122 | + |
| 123 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 124 | + |
| 125 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 126 | + |
| 127 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 128 | + |
0 commit comments