Let each node’s left child be the next smaller node and each node’s right child be the next bigger node.
Node prev = null; Node head = null; public Node treeToDoublyList(Node root) { if (root == null) { return null; } helper(root); head.left = prev; prev.right = head; return head; } private void helper(Node p) { if (p == null) { return; } helper(p.left); //handle current if (prev == null) { head = p; } else { prev.right = p; p.left = prev; } prev = p; helper(p.right); } |