中序遍历,使用两个指针,一个指针指向元素最小的节点(头节点),另一个指针指向当前遍历的节点(尾节点)
class Solution {
Node start = null;
Node end = null;
public Node treeToDoublyList(Node root) {
if(root == null) return root;
dfs(root);
end.right= start;
start.left = end;
return start;
}
private void dfs(Node root){
if(root.left != null) dfs(root.left);
if(start == null){
start = root;
end = root;
}else{
end.right = root;
root.left = end;
end = root;
}
if(root.right != null) dfs(root.right);
}
}