Binary Tree Traversals

You are provided with a binary tree consisting of integer-valued nodes. The task is to compute the In-Order, Pre-Order, and Post-Order traversals for the given binary tree.

Input:

The input begins with an integer 'T', representing the number of test cases.

For each test case, a line of integers is provided, denoting the tree's nodes in level order. Use -1 for any null node.

Output:

For each test case, return a nested list containing the three traversals (In-Order, Pre-Order, Post-Order).

Each traversal will be a list of integers, printed in separate lines in the specified order.

Example:

For a binary tree like the following:

[Image of a binary tree]

In-Order traversal: [5, 3, 2, 1, 7, 4, 6]
Pre-Order traversal: [1, 3, 5, 2, 4, 7, 6]
Post-Order traversal: [5, 2, 3, 7, 6, 4, 1]

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 3000
  • 0 ≤ data ≤ 10^9
Note:
- A non-null node from a previous level becomes the parent of the first two nodes of the current level.
- Input ends when nodes at the last level are all null (-1).
- The tree is represented in a single line for each test case.
AnswerBot
8d

Compute In-Order, Pre-Order, and Post-Order traversals for a given binary tree.

  • Implement tree traversal algorithms like In-Order, Pre-Order, and Post-Order.

  • Use recursion to traverse the binary tree ef...read more

Help your peers!
Add answer anonymously...
Cisco Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter