Binary search tree is given. Find the next element after the given one in the tree.
Definition of a tree:
// Java
class TreeNode
{
public:
int val;
TreeNode left, right, parent;
TreeNode(int x, TreeNode prev) {
val = x;
left = NULL;
right = NULL;
parent = prev;
};// C++
class TreeNode
{
public:
int val;
TreeNode *left, *right, *parent;
TreeNode(int x, TreeNode *prev = NULL) : val(x), left(NULL), right(NULL), parent(prev) {};
};Implement function Next that returns pointer to the next element in a tree. If next element does not exist, return NULL.
// Java
TreeNode Next(TreeNode tree)// C++
TreeNode* Next(TreeNode *tree)
Function Next with tree pointing to returns pointer to the node with value .