Extracting Leaf Nodes from a Binary Tree

Given a binary tree, develop a solution to retrieve a list of all the leaf nodes in the order they appear from left to right. If two leaf nodes are equidistant from the leftmost node, prioritize the one with lesser depth or, if necessary, the smaller node data.

Input:

Elements in level order format. The input consists of values of nodes separated by spaces in a single line. A placeholder of -1 indicates a null node.

Output:

Return a list containing all the leaf nodes of the binary tree, listed from left to right.

Example:

Consider the input that represents the binary tree depicted in the image below:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Explanation:

The root is 1.
Level 2 has nodes 2 (left) and 3 (right).
Level 3 has nodes 4 (left child of 2), 5 and 6 (children of 3).
Level 4 ends with node 7 (right child of 4) and nulls.
This sequence represents the tree structure where nodes are level-order traversed.

Constraints:

  • 0 <= N <= 105
  • 0 <= Node Value <= 108
  • 'N' is the total count of nodes in the BinaryTree.
  • Time Limit: 1 second
Note:
The listed format illustrates input preparation for a given tree structure. The complete sequence will be provided as a single line separated by spaces.
AnswerBot
4mo

Retrieve leaf nodes from a binary tree in left to right order, prioritizing lesser depth or smaller node data if equidistant from leftmost node.

  • Traverse the binary tree in level order and keep track o...read more

Help your peers!
Select
Add answer anonymously...

AlphaSense Software Developer interview questions & answers

A Software Developer was asked 11mo agoQ. Write a program to print the last 10 lines of a file, and configure it to run ev...read more
A Software Developer was asked 11mo agoQ. Let's discuss a system design scenario: Imagine an API that takes a long time to...read more
A Software Developer was asked Q. What is a peer-to-peer connection?

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. Let's discuss a system design scenario: Imagine an API that takes a long time to...read more
A Software Developer was asked Q2. What is a peer-to-peer connection?
A Software Developer was asked Q3. Median of Two Sorted Arrays Given two sorted arrays A and B of sizes N and M, fi...read more
AlphaSense Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits