Skip to content

Commit 0811db3

Browse files
authored
Create 0538-convert-bst-to-greater-tree.js
1 parent db64a15 commit 0811db3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* Tree | reverse pre-order-traversal
11+
* Time O(n) | Space O(n)
12+
* @param {TreeNode} root
13+
* @return {TreeNode}
14+
*/
15+
var convertBST = function(root) {
16+
17+
const dfs = (node, max) => {
18+
if(!node) return max;
19+
20+
const result = dfs(node.right, max);
21+
node.val = result + node.val;
22+
const result1 = dfs(node.left, node.val);
23+
return result1;
24+
}
25+
26+
dfs(root, 0);
27+
return root;
28+
};

0 commit comments

Comments
 (0)