LeetCode: Convert BST to Greater Tree

Posted ying_vincent

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode: Convert BST to Greater Tree相关的知识,希望对你有一定的参考价值。

利用好BST的特性,解起来并不难

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     private int sum = 0;
12     public TreeNode convertBST(TreeNode root) {
13         if (root == null) return null;
14         convertBST(root.right);
15         root.val += sum;
16         sum = root.val;
17         convertBST(root.left);
18         return root;
19     }
20 }

 

以上是关于LeetCode: Convert BST to Greater Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode - Convert BST to Greater Tree

LeetCode:Convert BST to Greater Tree

Leetcode 538. Convert BST to Greater Tree

[LeetCode] 538. Convert BST to Greater Tree

LeetCode: Convert BST to Greater Tree

LeetCode算法题-Convert BST to Greater Tree(Java实现)