e-olymp
favorite We need a little bit of your help to keep things running, click on this banner to learn more
Competitions

class Tree

Tree Find

Binary search tree is given. Find element in a tree.

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 Find that returns pointer to the element found. If element does not exist, return NULL.

// Java
TreeNode Find(TreeNode tree, int element)
// C++
TreeNode* Find(TreeNode *tree, int element)

Example

prb10057.gif

Function Find with element = 9 returns pointer to the node with value 9.

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev