# LeetCode: Convert Bst To Greater Tree Solution

## Approach

Reversed in-order traverse (right to left)

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**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 * @param {TreeNode} root11 * @return {TreeNode}12 */13var convertBST = function (root) {14  let sum = 015
16  const traverse = function (node) {17    if (!node) {18      return19    }20
21    traverse(node.right)22    sum += node.val23    node.val = sum24    traverse(node.left)25  }26
27  traverse(root)28  return root29}```

## Comments

Loading comments...

leetcode

tree

recursion

## Next Post

CodeWars: The Soul Of Wit Reverse An Array

Feb 10, 2021

Search Posts