mediumBinary Trees 0 views

Print Left View of a Binary Tree

Print the first node visible at each level of the sample tree, extending level order traversal with a single boolean flag.

This exercise builds directly on Binary Tree Level Order Traversal. The left view of a binary tree is what you'd see if you looked at the tree from the left side: at every level, only the first node encountered is visible, and everything else at that level is hidden behind it.

"First node encountered" means the first node in level-order (breadth-first) order -- so this is level order traversal with one twist: print only the first node at each level and skip the rest until the next level starts.

The idea

Reuse the exact queue + null sentinel setup from level order, and add a single boolean flag:

  1. Set a flag bPrint to true at the start of each level (right when a fresh null marker is enqueued).
  2. When a real node is dequeued: if bPrint is still true, print it and flip the flag to false. Every other node in that level gets skipped over for printing -- but its children still get enqueued, since they're needed to process the next level.
  3. When the null marker is dequeued, a level has just finished: reset bPrint back to true for the level that's about to start (same empty-queue check as level order to know when to stop).
public void printLeftView(){
    if(root == null){
        return;
    }

    Queue<Node<T>> queue = new LinkedList<>();
    queue.add(root);
    queue.add(null);
    boolean bPrint = true;

    while(!queue.isEmpty()){
        Node<T> node = queue.remove();

        if(node != null){
            if(bPrint){
                System.out.println(node.data);
                bPrint = false;
            }
            if(node.left!= null){
                queue.add(node.left);
            }
            if(node.right!= null){
                queue.add(node.right);
            }
        }
        else{
            // We have reached to a new level
            System.out.println();
            if(queue.isEmpty()){
                break;
            }
            queue.add(null);
            bPrint = true;
        }
    }
}

The sample tree

1
|- left: 2
|    |- left: 4
|    |    `- left: 8
|    `- right: 5
|         `- left: 9
`- right: 3
     |- left: 6
     `- right: 7
          |- left: 10
          `- right: 11
               `- right: 12

See the Solutions tab for the full Java implementation.

This is a foundational exercise -- there are no graded test cases for it, since printLeftView() prints rather than returns a value. The level-by-level output below (verified by hand against the tree above) serves as a worked example instead.

Example 1

Input: printLeftView() on the sample tree -- level 0

Output: 1

Explanation: Only node at this level.

Example 2

Input: level 1

Output: 2

Explanation: First of 2 and 3.

Example 3

Input: level 2

Output: 4

Explanation: First of 4, 5, 6, 7.

Example 4

Input: level 3

Output: 8

Explanation: First of 8, 9, 10, 11.

Example 5

Input: level 4

Output: 12

Explanation: Only node at this level -- and it comes from the right subtree, since the left subtree doesn't reach this deep.

Constraints

  • Uses the same 12-node sample tree as Binary Tree Basics.
  • Prints exactly one node per level -- the first one encountered in level order, which is not always the structurally leftmost node.

Follow-up

Later exercises in the Binary Trees series (height/depth, balancing, search) will also operate on this same tree.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

This question doesn't have a code exercise.