您的位置:首页 > 其它

LeetCode: Convert BST to Greater Tree

2017-05-12 11:01 387 查看
利用好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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: