Tree Maximum element#10062

Tree Maximum element

1 second
128 megabytes
Very easy

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

Definition of a tree:

class TreeNode {
  public int val;
  public TreeNode left;
  public TreeNode right;
  
  TreeNode(int x) {
    val = x;
    left = null; 
    right = null;
  }
}
class TreeNode {
public:
  int val;
  TreeNode *left;
  TreeNode *right;
  
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

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

TreeNode Maximum(TreeNode tree)
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.