Question
Node Search in a Binary Tree
Given the root of a binary tree and an integer key, write a function to determine whether the key exists in the tree.
The function should return:
-
True
if the key is found in any node of the binary tree. -
False
if the key does not exist in the tree.
Input
User Task
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function node_find(), which takes the root node and an integer value key as its parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-negative values represent node values, and -1 indicates a null node.
Note: Once a node is marked as -1, no further information about its children is provided.
The last line of the input contains a positive integer key, representing the value that needs to be searched.
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function node_find(), which takes the root node and an integer value key as its parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-negative values represent node values, and -1 indicates a null node.
Note: Once a node is marked as -1, no further information about its children is provided.
The last line of the input contains a positive integer key, representing the value that needs to be searched.
Output
Return True if the key is found in any node of the binary tree, otherwise return False.
Example
Input
9
1 2 3 4 -1 5 6 -1 7 8 9
8
Output
True
Explanation
The below given tree represents the binary tree described in the input:
8 exists in the given binary tree hence we return True.
9
1 2 3 4 -1 5 6 -1 7 8 9
8
Output
True
Explanation
The below given tree represents the binary tree described in the input:

8 exists in the given binary tree hence we return True.