X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Next Right Node
Easy Accuracy: 42.4% Submissions: 5121 Points: 2

Given a Binary tree and a key in the binary tree, find the node right to the given key. If there is no node on right side, then return a node with value -1.

Example 1:

Input:
          10
     /    \
    2      6
   / \      \
  8   4      5
and key = 2
Output: 6
Explanation: We can see in the above tree
that the next right node of 2 is 6.

Example 2:

Input:
         10
    /     \
   2       6
  / \       \
 8   4       5
and key = 5
Output: -1

Your Task:
You don't need to read input or print anything. Your task is to complete the function nextRight() which takes root node of the tree and an integer key as input parameters and returns the next right node of the node with value key. 


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1<=N<=103
1<=data of node<=103
1<=key<=103

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Next Right Node

Output Window