Skip to content

Commit ef22908

Browse files
authored
Create 1038-binary-search-tree-to-greater-sum-tree.js
1 parent 830af7a commit ef22908

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {TreeNode}
11+
*/
12+
const bstToGst = function(root) {
13+
const arr = []
14+
dfs(root, arr)
15+
let v = 0
16+
for(let i = arr.length - 1; i >= 0; i--) {
17+
arr[i].val = arr[i].val + v
18+
v = arr[i].val
19+
}
20+
return root
21+
};
22+
23+
function dfs(node, arr) {
24+
if(node == null) return
25+
dfs(node.left, arr)
26+
arr.push(node)
27+
dfs(node.right, arr)
28+
}

0 commit comments

Comments
 (0)