Postorder Successor Problem Statement

You are provided with a binary tree consisting of 'N' distinct nodes and an integer 'M'. Your task is to find and return the postorder successor of 'M'.

Note:

The postorder successor of a node 'M' is the subsequent element to 'M' in the sequence resulting from postorder tree traversal.

If in postorder traversal the sequence is 3 5 4 7, then clearly, the postorder successor of 5 is 4. If there is no postorder successor of 'M', return '-1'.

Input:

The first line consists of an integer 'T' representing the number of test cases.

Then, each test case starts with a line containing the elements of the tree presented in level order format, with values of nodes separated by a space. In case of a NULL node, '-1' is used.
The second line of each test case provides 'M', the node whose postorder successor must be discovered.

Example:
1
3 8
5 2 7 -1
-1 -1 -1 -1 -1 -1

Output:

Output an integer representing the post-order successor of node 'M' in the binary tree.
Each test case result should be printed on a new line.

Example:

Input:
1 3 8 5 2 7 -1 -1 -1 -1 -1 -1 1
Output:
3

Constraints:

  • 1 <= 'T' <= 5
  • 1 <= 'N' <= 3000
  • 1 <= 'M' <= 3000
  • 1 <= nodeVal <= 109
  • Time Limit: 1 sec

Note:

You need not print anything; handle the implementation as specified in the problem.

AnswerBot
2d

Find the postorder successor of a node in a binary tree.

  • Traverse the binary tree in postorder to get the sequence.

  • Find the index of the node 'M' in the postorder sequence.

  • Return the element at the nex...read more

Help your peers!
Add answer anonymously...
Analytic Edge Front end Developer 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