Bird
Raised Fist0
DSA Javaprogramming~10 mins

Boundary Traversal of Binary Tree in DSA Java - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the root node's data in the boundary traversal.

DSA Java
System.out.print(root[1]);
Drag options to blanks, or click blank then click option'
A.data
B.left
C.right
D.val
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.
2fill in blank
medium

Complete the code to check if the current node is a leaf node.

DSA Java
if(node.left == null && node[1] == null) {
Drag options to blanks, or click blank then click option'
A.val
B.parent
C.right
D.data
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.data or node.val instead of children.
Using node.parent which is not relevant here.
3fill in blank
hard

Fix 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;
}
Drag options to blanks, or click blank then click option'
A.data
B.right
C.parent
D.val
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.
4fill in blank
hard

Fill 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);
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.data
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.data or node.parent in recursive calls.
Checking leaf condition incorrectly.
5fill in blank
hard

Fill 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() + " ");
    }
}
Drag options to blanks, or click blank then click option'
A.right
B.data
C.left
D.parent
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.

Practice

(1/5)
1. What is the correct order of nodes visited in a boundary traversal of a binary tree?
easy
A. Root, leaves (left to right), left boundary (top-down), right boundary (bottom-up)
B. Leaves (left to right), root, left boundary (top-down), right boundary (bottom-up)
C. Root, left boundary (top-down), leaves (left to right), right boundary (bottom-up)
D. Left boundary (top-down), root, right boundary (bottom-up), leaves (left to right)

Solution

  1. Step 1: Understand boundary traversal parts

    Boundary traversal visits root, then left boundary top-down, then leaves left to right, then right boundary bottom-up.
  2. Step 2: Arrange traversal order

    Root is first, left boundary next, leaves after that, and right boundary last in reverse order.
  3. Final Answer:

    Root, left boundary (top-down), leaves (left to right), right boundary (bottom-up) -> Option C
  4. Quick Check:

    Boundary traversal order = Root + Left + Leaves + Right [OK]
Hint: Remember: Root, Left boundary, Leaves, Right boundary [OK]
Common Mistakes:
  • Mixing order of leaves and boundaries
  • Visiting right boundary top-down instead of bottom-up
  • Skipping root node
2. Which of the following Java method signatures is correct for a function performing boundary traversal on a binary tree node?
easy
A. public void boundaryTraversal() Node root
B. void boundaryTraversal(Node root) public
C. public boundaryTraversal(Node root) void
D. public void boundaryTraversal(Node root)

Solution

  1. Step 1: Recall Java method syntax

    Java methods start with access modifier, then return type, then method name and parameters.
  2. Step 2: Match correct syntax

    public void boundaryTraversal(Node root) matches: public void boundaryTraversal(Node root). Others have wrong order or missing parts.
  3. Final Answer:

    public void boundaryTraversal(Node root) -> Option D
  4. Quick Check:

    Access modifier + return type + method name + parameters [OK]
Hint: Java method = access modifier + return type + name + params [OK]
Common Mistakes:
  • Placing return type after method name
  • Missing access modifier
  • Incorrect parameter placement
3. Given the binary tree below, what is the output of the boundary traversal?

        1
       / \
      2   3
     / \   \
    4   5   6
       / \  /
      7  8 9
medium
A. [1, 2, 4, 7, 8, 9, 6]
B. [1, 2, 4, 7, 8, 9, 6, 3]
C. [1, 2, 4, 5, 7, 8, 9, 6, 3]
D. [1, 2, 4, 7, 8, 9, 3, 6]

Solution

  1. Step 1: Identify boundary nodes

    Root: 1; Left boundary top-down: 2 (excluding leaf 4); Leaves left to right: 4,7,8,9; Right boundary bottom-up: 6,3 (excluding leaves).
  2. Step 2: Combine nodes without duplicates

    Boundary traversal: 1 (root), 2 (left boundary), 4,7,8,9 (leaves), 6,3 (right boundary).
  3. Final Answer:

    [1, 2, 4, 7, 8, 9, 6, 3] -> Option B
  4. Quick Check:

    Boundary traversal output = [1,2,4,7,8,9,6,3] [OK]
Hint: Leaves included once; right boundary reversed; avoid duplicates [OK]
Common Mistakes:
  • Including leaf nodes twice
  • Wrong order of right boundary nodes
  • Missing leaf nodes
4. Consider this Java method snippet for boundary traversal:

void printLeftBoundary(Node node) {
  if (node == null) return;
  System.out.print(node.data + " ");
  if (node.left != null) {
    printLeftBoundary(node.left);
  } else if (node.right != null) {
    printLeftBoundary(node.right);
  }
}
What is the bug in this code?
medium
A. It prints leaf nodes which should be excluded in left boundary
B. It does not print the root node
C. It causes infinite recursion on leaf nodes
D. It prints right boundary nodes instead of left

Solution

  1. Step 1: Analyze leaf node printing

    The code prints node data before checking for children, thus printing leaf nodes as well.
  2. Step 2: Identify leaf node exclusion missing

    Leaf nodes should not be printed in left boundary function, but this code prints them unconditionally after null check.
  3. Final Answer:

    It prints leaf nodes which should be excluded in left boundary -> Option A
  4. Quick Check:

    Left boundary excludes leaves, code prints them [OK]
Hint: Exclude leaf nodes in boundary functions to avoid duplicates [OK]
Common Mistakes:
  • Not checking if node is leaf before printing
  • Confusing left and right boundary logic
  • Missing base case for null nodes
5. You have a binary tree where some nodes have only right children, and leaves are only on the rightmost path. How should you modify the boundary traversal algorithm to correctly print the boundary without duplicates?
hard
A. Skip printing left boundary if no left child; print leaves only once; print right boundary bottom-up excluding leaves
B. Print all nodes in preorder to cover all boundaries and leaves
C. Print left boundary top-down including leaves; skip right boundary
D. Print leaves first, then left and right boundaries in any order

Solution

  1. Step 1: Understand tree structure

    Since leaves are only on rightmost path and some nodes have only right children, left boundary may be missing or partial.
  2. Step 2: Modify traversal rules

    Skip left boundary if no left child; print leaves once; print right boundary bottom-up excluding leaves to avoid duplicates.
  3. Step 3: Combine for correct boundary

    This ensures no duplicates and correct order for such skewed trees.
  4. Final Answer:

    Skip printing left boundary if no left child; print leaves only once; print right boundary bottom-up excluding leaves -> Option A
  5. Quick Check:

    Handle missing left boundary and leaf duplicates carefully [OK]
Hint: Skip missing boundaries; print leaves once; reverse right boundary [OK]
Common Mistakes:
  • Printing leaves multiple times
  • Ignoring missing left boundary
  • Printing right boundary top-down causing wrong order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard