Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the root node's data in the boundary traversal.
DSA Java
System.out.print(root[1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.left or root.right instead of root.data.
Using root.val which is not defined in this node structure.
✗ Incorrect
The root node's data is accessed using root.data in Java binary tree nodes.
2fill in blank
mediumComplete the code to check if the current node is a leaf node.
DSA Java
if(node.left == null && node[1] == null) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.data or node.val instead of children.
Using node.parent which is not relevant here.
✗ Incorrect
A leaf node has no left or right children, so both must be null.
3fill in blank
hardFix the error in the code to traverse the left boundary excluding leaves.
DSA Java
while(current != null && !(current.left == null && current[1] == null)) { System.out.print(current.data + " "); if(current.left != null) current = current.left; else current = current.right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking current.data or current.val instead of children.
Using current.parent which is not part of this check.
✗ Incorrect
To check if current is a leaf, both left and right children must be null.
4fill in blank
hardFill both blanks to collect leaf nodes in left-to-right order.
DSA Java
void printLeaves(Node node) {
if(node == null) return;
printLeaves(node[1]);
if(node.left == null && node[2] == null) System.out.print(node.data + " ");
printLeaves(node.right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using node.data or node.parent in recursive calls.
Checking leaf condition incorrectly.
✗ Incorrect
Traverse left subtree first, then check leaf, then right subtree.
5fill in blank
hardFill all three blanks to traverse the right boundary in bottom-up order excluding leaves.
DSA Java
void printRightBoundary(Node node) {
Node current = node.right;
Stack<Integer> stack = new Stack<>();
while(current != null && !(current.left == null && current[1] == null)) {
stack.push(current[2]);
if(current.right != null) current = current.right;
else current = current[3];
}
while(!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Pushing current.left or current.right instead of current.data.
Moving to parent instead of left child when right child is null.
✗ Incorrect
Check if current.left and current.right are null to detect leaf; push current.data; move to right child if exists else left.
