Inorder Successor in a Binary Tree

Given a node in an arbitrary binary tree, find its inorder successor. The successor is defined as the node that appears immediately after the given node in the in-order traversal of the tree. If the given node is the last node in this traversal, the successor should be NULL.

Input:

The input will have the following format:

The first line contains an integer 'T', the number of test cases.
Each test case consists of:
- A single line with integers representing the binary tree in level order (use -1 for NULL nodes).
- A single integer representing the value of the node for which the inorder successor is to be found.

Output:

For each test case, output a single line that contains the value of the inorder successor node, or 'NULL' if no inorder successor exists.

Example:

Consider the example of a binary tree given by levels:

altImage
Input:
4 -1 5 6
5
Output:
6

This shows that the inorder successor of node 5 in the given tree is 6.

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 103
  • 1 <= NODEVALUE <= 109
  • Time Limit: 1 sec.

Note:

Each node has a unique integer value. The node for which the successor is to be found is guaranteed to be part of the tree. Implementation is required only for finding the successor, as input/output handling is already managed.
AnswerBot
4mo

Given a node in a binary tree, find its inorder successor in the tree.

  • Traverse the tree in in-order fashion to find the successor node.

  • If the given node has a right child, the successor will be the le...read more

Help your peers!
Select
Add answer anonymously...

SAP Developer Associate interview questions & answers

A Developer Associate was asked 2mo agoQ. Have you worked with CDS views and implemented the RAP framework?
A Developer Associate was asked Q. What are binary trees?
A Developer Associate was asked Q. Design a calculator that performs operations on 128-bit integers.

Popular interview questions of Developer Associate

A Developer Associate was asked Q1. What are binary trees?
A Developer Associate was asked Q2. Design a calculator that performs operations on 128-bit integers.
A Developer Associate was asked Q3. How to reverse a linked list and write program to get right view of a binary tre...read more
SAP Developer Associate 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