
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find Inorder Successor of a binary search tree in C++
Suppose we have a binary search tree BST and another value of a node, we have to find the in-order successor of that node in the BST. As we all know that the successor of a node p is the node with the smallest key greater than the value of p.
So, if the input is like
And p = 1, then the output will be 2,
To solve this, we will follow these steps −
- Define recursive method inorderSuccessor(), this will take root and p
- if root null, then:
- return null
- if val of root <= val of p, then:
- return inorderSuccessor(right of root , p)
- Otherwise
- option := inorderSuccessor(left of root , p)
- return (if option is zero, then root, otherwise option)
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; class Solution { public: TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { if(!root) return NULL; if(root->val <= p->val){ return inorderSuccessor(root->right, p); }else{ TreeNode* option = inorderSuccessor(root->left, p); return !option ? root : option; } } }; main(){ TreeNode *root = new TreeNode(2); root->left = new TreeNode(1); root->right = new TreeNode(3); TreeNode *p = root->left; Solution ob; cout << (ob.inorderSuccessor(root, p))->val; }
Input
TreeNode *root = new TreeNode(2); root->left = new TreeNode(1); root->right = new TreeNode(3); 1
Output
2
- Related Articles
- Program to perform an Inorder Traversal of a binary tree in Python
- Replace Each Node in Binary Tree With The Sum Of Its Inorder Predecessor And Successor Using C++
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- Check if an array represents Inorder of Binary Search tree or not in Python
- C++ Program to Find Lowest Common Ancestor in a Binary Search Tree
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program to Find the Minimum value of Binary Search Tree
- Inorder Traversal of a Threaded Binary Tree in C++
- Binary Tree Inorder Traversal in Python
- Preorder Successor of a Node in Binary Tree in C++
- Postorder successor of a Node in Binary Tree in C++
- C++ Program to Search for an Element in a Binary Search Tree
- Program to find largest binary search subtree from a given tree in Python
- Python Program to Sort using a Binary Search Tree
- Binary Tree to Binary Search Tree Conversion in C++

Advertisements