forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: azl397985856#92 增加注释 #236增加扩展 backlog增加题目
- Loading branch information
luzhipeng
committed
Apr 30, 2019
1 parent
9782d62
commit 2f9b3bb
Showing
3 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* @lc app=leetcode id=538 lang=javascript | ||
* | ||
* [538] Convert BST to Greater Tree | ||
* | ||
* https://leetcode.com/problems/convert-bst-to-greater-tree/description/ | ||
* | ||
* algorithms | ||
* Easy (50.04%) | ||
* Total Accepted: 75.4K | ||
* Total Submissions: 149K | ||
* Testcase Example: '[5,2,13]' | ||
* | ||
* Given a Binary Search Tree (BST), convert it to a Greater Tree such that | ||
* every key of the original BST is changed to the original key plus sum of all | ||
* keys greater than the original key in BST. | ||
* | ||
* | ||
* Example: | ||
* | ||
* Input: The root of a Binary Search Tree like this: | ||
* 5 | ||
* / \ | ||
* 2 13 | ||
* | ||
* Output: The root of a Greater Tree like this: | ||
* 18 | ||
* / \ | ||
* 20 13 | ||
* | ||
* | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {TreeNode} | ||
*/ | ||
var convertBST = function(root) { | ||
let res = 0; | ||
function r(root) { | ||
if (root === null) return null; | ||
|
||
r(root.right); | ||
|
||
root.val += res; | ||
|
||
res = +root.val; | ||
|
||
r(root.left); | ||
|
||
return root; | ||
} | ||
r(root); | ||
return root; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters