mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-10-26 07:18:56 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			38 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <p>Given the <code>root</code> of 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 the sum of all keys greater than the original key in BST.</p>
 | |
| 
 | |
| <p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>
 | |
| 
 | |
| <ul>
 | |
| 	<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node's key.</li>
 | |
| 	<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node's key.</li>
 | |
| 	<li>Both the left and right subtrees must also be binary search trees.</li>
 | |
| </ul>
 | |
| 
 | |
| <p> </p>
 | |
| <p><strong>Example 1:</strong></p>
 | |
| <img alt="" src="https://assets.leetcode.com/uploads/2019/05/02/tree.png" style="width: 500px; height: 341px;" />
 | |
| <pre>
 | |
| <strong>Input:</strong> root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
 | |
| <strong>Output:</strong> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
 | |
| </pre>
 | |
| 
 | |
| <p><strong>Example 2:</strong></p>
 | |
| 
 | |
| <pre>
 | |
| <strong>Input:</strong> root = [0,null,1]
 | |
| <strong>Output:</strong> [1,null,1]
 | |
| </pre>
 | |
| 
 | |
| <p> </p>
 | |
| <p><strong>Constraints:</strong></p>
 | |
| 
 | |
| <ul>
 | |
| 	<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
 | |
| 	<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
 | |
| 	<li>All the values in the tree are <strong>unique</strong>.</li>
 | |
| 	<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
 | |
| </ul>
 | |
| 
 | |
| <p> </p>
 | |
| <p><strong>Note:</strong> This question is the same as 1038: <a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/" target="_blank">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>
 |