本代码基于树的中序遍历非递归版
class Solution {
public TreeNode convert(TreeNode root) {
if(root==null) return null;
Stack<TreeNode> stack=new Stack();
List<TreeNode> list=new LinkedList();
TreeNode cur=root;
while(!stack.empty() || cur!=null){
if(cur!=null){
stack.push(cur);
cur=cur.left;
}else{
TreeNode node=stack.pop();
int size=list.size();
if(0==size){
node.left=null;
}else{
TreeNode preNode=list.get(size-1);
preNode.right=node;
node.left=preNode;
}
list.add(node);
cur=node.right;
}
}
list.get(list.size()-1).right=null;
return list.get(0);
}
}