Problem: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/
Soluiton:
使用inorder traversal读取值,读出来的node和cur double linked
最后再把dummy node删除
class Solution {
public Node treeToDoublyList(Node root) {
// inorder traverse
Stack<Node> stack = new Stack<>();
if(root == null) return null;
Node dummy = new Node(0);
Node cur = dummy;
Node temp = root;
while(!stack.isEmpty() || temp != null) {
while(temp != null) {
stack.push(temp);
temp = temp.left;
}
temp = stack.pop();
cur.right = temp;
temp.left = cur;
temp = temp.right;
cur = cur.right;
}
cur.right = dummy.right;
dummy.right.left = cur;
cur = cur.right;
return cur;
}
}