码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode: Convert BST to Greater Tree

时间:2017-05-12 11:44:14      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:node   利用   return   roo   style   ini   span   pre   div   

利用好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

标签:node   利用   return   roo   style   ini   span   pre   div   

原文地址:http://www.cnblogs.com/yingzhongwen/p/6844549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!