Problem Statement
Populate Inorder Successor for all nodes | Practice | GeeksforGeeks
Pattern:
Solution
public void populateNext(Node root){
Deque<Node> stack = new LinkedList<>();
Node node = root, pre = null;
while(node!=null || !stack.isEmpty()) {
if(node!=null){
stack.push(node);
node = node.left;
} else {
node = stack.pop();
if(pre!=null) pre.next = node;
pre = node;
node = node.right;
}
}
}
Notes
- jsut a laggin pointer, pointing its next to the curr
node
ptr