思路:二叉树的中序遍历是递增的,因此会想到中序遍历。
class Solution {
Node pre, head;
public Node treeToDoublyList(Node root) {
if(root == null) return null;
inorder(root);
head.left = pre;
pre.right = head;
return head;
}
void inorder(Node root){
if (root == null) return;
// 左
inorder(root.left);
if (pre == null){
head = root;
}else {
pre.right = root;
root.left = pre;
}
pre = root;
// 右
inorder(root.right);
}
}