用stack迭代 先算右子树 在左子树
classSolution{public TreeNode convertBST(TreeNode root){int sum =0;
TreeNode node = root;
Stack<TreeNode> stack =newStack<TreeNode>();while(!stack.isEmpty()|| node != null){/* push all nodes up to (and including) this subtree's maximum on
* the stack. */while(node != null){
stack.add(node);
node = node.right;}
node = stack.pop();
sum += node.val;
node.val = sum;/* all nodes with values between the current and its parent lie in
* the left subtree. */
node = node.left;}return root;}}
classSolution{privateint sum =0;public TreeNode convertBST(TreeNode root){traver(root);return root;}privatevoidtraver(TreeNode node){if(node == null)return;traver(node.right);
sum += node.val;
node.val = sum;traver(node.left);}}