eolymp
bolt
Try our new interface for solving problems
Problems

Tree Maximum element

Tree Maximum element

Time limit 1 second
Memory limit 128 MiB

Binary search tree is given. Return the pointer to the maximum element.

Definition of a tree:

// Java
class TreeNode
{
public:
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) {
    val = x;
    left = NULL; 
    right = NULL;
};

// C++
class TreeNode
{
public:
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

Implement function Maximum that returns pointer to the element with maximum value in the tree.

// Java
TreeNode Maximum(TreeNode tree)
// C++
TreeNode* Maximum(TreeNode *tree)

Example

prb10057.gif

Function Maximum returns pointer to the node with value 16 - the vertex with maximum value in the tree.

Author Mykhailo Medvediev