Tree Find#10063

Tree Find

1 second
128 megabytes
Very easy

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)

Examples

prb10057.gif

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