We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 树, 深度优先搜索, 二叉搜索树, 二叉树
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
node
node.val
提醒一下,二叉搜索树满足下列约束条件:
**注意:**本题和 1038: 相同
示例 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]
提示:
0
Language: JavaScript
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {TreeNode} */ var convertBST = function(root) { let sum = 0 function traversal (root) { if (root) { traversal(root.right) sum += root.val root.val = sum traversal(root.left) } } traversal(root) return root }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
538. 把二叉搜索树转换为累加树
Description
Difficulty: 中等
Related Topics: 树, 深度优先搜索, 二叉搜索树, 二叉树
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点
node
的新值等于原树中大于或等于node.val
的值之和。提醒一下,二叉搜索树满足下列约束条件:
**注意:**本题和 1038: 相同
示例 1:
示例 2:
示例 3:
示例 4:
提示:
0
和 104之间。Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: