# Check if there is a root to leaf path with given sequence

Given a binary tree and an array, the task is to find if the given array sequence is present as a root to leaf path in given tree.

Examples :

```Input : arr[] = {5, 2, 4, 8} for above tree
Output: "Path Exist"

Input :  arr[] = {5, 3, 4, 9} for above tree
Output: "Path does not Exist"
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution for this problem is to find all root to leaf paths in given tree and for each root to leaf path check that path and given sequence in array both are identical or not.

An efficient solution for this problem is to traverse the tree once and while traversing the tree we have to check that if path from root to current node is identical to the given sequence of root to leaf path. Here is the algorithm :

• Start traversing tree in preorder fashion.
• Whenever we moves down in tree then we also move by one index in given sequence of root to leaf path .
• If current node is equal to the arr[index] this means that till this level of tree path is identical.
• Now remaining path will either be in left subtree or in right subtree.
• If any node gets mismatched with arr[index] this means that current path is not identical to the given sequence of root to leaf path, so we return back and move in right subtree.
• Now when we are at leaf node and it is equal to arr[index] and there is no further element in given sequence of root to leaf path, this means that path exist in given tree.
• ## C++

 `// C++ program to see if there is a root to leaf path ` `// with given sequence. ` `#include ` `using` `namespace` `std; ` ` `  `/* A binary tree node has data, pointer to left child ` `and a pointer to right child */` `struct` `Node ` `{ ` `    ``int` `data; ` `    ``struct` `Node* left, *right; ` `}; ` ` `  `/* utility that allocates a new node with the ` `given data and NULL left and right pointers. */` `struct` `Node* newnode(``int` `data) ` `{ ` `    ``struct` `Node* node = ``new` `Node; ` `    ``node->data = data; ` `    ``node->left = node->right  = NULL; ` `    ``return` `(node); ` `} ` ` `  `// function to check given sequence of root to leaf path exist ` `// in tree or not. ` `// index represents current element in sequence of rooth to ` `// leaf path ` `bool` `existPath(``struct` `Node *root, ``int` `arr[], ``int` `n, ``int` `index) ` `{ ` `    ``// If root is NULL, then there must not be any element ` `    ``// in array. ` `    ``if` `(root == NULL) ` `        ``return` `(n == 0); ` ` `  `   ``// If this node is a leaf and matches with last entry ` `   ``// of array. ` `   ``if` `((root->left == NULL && root->right == NULL) && ` `       ``(root->data == arr[index]) && (index == n-1)) ` `            ``return` `true``; ` ` `  `   ``// If current node is equal to arr[index] this means ` `   ``// that till this level path has been matched and ` `   ``// remaining path can be either in left subtree or ` `   ``// right subtree. ` `   ``return` `((index < n) && (root->data == arr[index]) && ` `              ``(existPath(root->left, arr, n,  index+1) || ` `               ``existPath(root->right, arr, n, index+1) )); ` `} ` ` `  `// Driver function to run the case ` `int` `main() ` `{ ` `    ``// arr[] --> sequence of root to leaf path ` `    ``int` `arr[] = {5, 8, 6, 7}; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]); ` `    ``struct` `Node *root = newnode(5); ` `    ``root->left    = newnode(3); ` `    ``root->right   = newnode(8); ` `    ``root->left->left = newnode(2); ` `    ``root->left->right = newnode(4); ` `    ``root->left->left->left = newnode(1); ` `    ``root->right->left = newnode(6); ` `    ``root->right->left->right = newnode(7); ` ` `  `    ``existPath(root, arr, n, 0)? cout << ``"Path Exists"` `: ` `                                ``cout << ``"Path does not Exist"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to see if there is a root to leaf path  ` `// with given sequence.  ` `public` `class` `CheckForPath { ` ` `  `    ``// function to check given sequence of root to leaf path exist  ` `    ``// in tree or not.  ` `    ``// index represents current element in sequence of rooth to  ` `    ``// leaf path  ` `    ``public` `static` `boolean` `existPath(Node root, ``int` `arr[], ``int` `index) ` `    ``{ ` `        ``// If root is NULL, then there must not be any element  ` `        ``// in array.  ` `        ``if``(root==``null``) ` `        ``{ ` `            ``return` `arr.length==``0``; ` `        ``} ` ` `  `        ``// If this node is a leaf and matches with last entry  ` `        ``// of array.  ` `        ``if``((root.left==``null` `&& root.right==``null``) && (root.data==arr[index]  ` `                                        ``&& root.data==arr[arr.length-``1``])) ` `        ``{ ` `            ``return` `true``; ` `        ``} ` ` `  `        ``// If current node is equal to arr[index] this means  ` `        ``// that till this level path has been matched and  ` `        ``// remaining path can be either in left subtree or  ` `        ``// right subtree.  ` `        ``return` `(index

## Python3

 `# Python3 program to see if there is a  ` `# root to leaf path with given sequence.  ` ` `  `# utility that allocates a new node with the  ` `# given data and None left and right pointers.  ` `class` `newnode:  ` `    ``def` `__init__(``self``, data): ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `self``.right ``=` `None` ` `  `# function to check given sequence of root  ` `# to leaf path exist in tree or not.  ` `# index represents current element in  ` `# sequence of rooth to leaf path  ` `def` `existPath(root, arr, n, index): ` `     `  `    ``# If root is None, then there must  ` `    ``# not be any element in array.  ` `    ``if` `(root ``=``=` `None``): ` `        ``return` `(n ``=``=` `0``) ` ` `  `# If this node is a leaf and matches  ` `# with last entry of array.  ` `    ``if` `((root.left ``=``=` `None` `and` `root.right ``=``=` `None``) ``and` `        ``(root.data ``=``=` `arr[index]) ``and` `(index ``=``=` `n ``-` `1``)):  ` `        ``return` `True` ` `  `# If current node is equal to arr[index] ` `# this means that till this level path  ` `# has been matched and remaining path  ` `# can be either in left subtree or  ` `# right subtree.  ` `    ``return` `((index < n) ``and` `(root.data ``=``=` `arr[index]) ``and`  `            ``(existPath(root.left, arr, n, index ``+` `1``) ``or` `             ``existPath(root.right, arr, n, index ``+` `1``))) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# arr[] -. sequence of root to leaf path  ` `    ``arr ``=` `[``5``, ``8``, ``6``, ``7``]  ` `    ``n ``=` `len``(arr)  ` `    ``root ``=` `newnode(``5``)  ` `    ``root.left ``=` `newnode(``3``)  ` `    ``root.right ``=` `newnode(``8``)  ` `    ``root.left.left ``=` `newnode(``2``)  ` `    ``root.left.right ``=` `newnode(``4``)  ` `    ``root.left.left.left ``=` `newnode(``1``)  ` `    ``root.right.left ``=` `newnode(``6``)  ` `    ``root.right.left.right ``=` `newnode(``7``)  ` ` `  `    ``if` `existPath(root, arr, n, ``0``): ` `        ``print``(``"Path Exists"``)  ` `    ``else``: ` `        ``print``(``"Path does not Exist"``) ` `         `  `# This code is contributed by PranchalK `

## C#

 `// C# program to see if there ` `// is a root to leaf path  ` `// with given sequence.  ` `using` `System; ` ` `  `public` `class` `CheckForPath  ` `{ ` ` `  `    ``// function to check given sequence ` `    ``// of root to leaf path exist  ` `    ``// in tree or not.  ` `    ``// index represents current element ` `    ``// in sequence of rooth to  ` `    ``// leaf path  ` `    ``public` `static` `bool` `existPath(Node root, ` `                        ``int` `[]arr, ``int` `index) ` `    ``{ ` `        ``// If root is NULL, then there ` `        ``// must not be any element  ` `        ``// in array.  ` `        ``if``(root == ``null``) ` `        ``{ ` `            ``return` `arr.Length == 0; ` `        ``} ` ` `  `        ``// If this node is a leaf and matches with last entry  ` `        ``// of array.  ` `        ``if``((root.left == ``null` `&& root.right == ``null``) &&  ` `                                ``(root.data == arr[index] && ` `                                ``root.data == arr[arr.Length - 1])) ` `        ``{ ` `            ``return` `true``; ` `        ``} ` ` `  `        ``// If current node is equal to arr[index] this means  ` `        ``// that till this level path has been matched and  ` `        ``// remaining path can be either in left subtree or  ` `        ``// right subtree.  ` `        ``return` `(index < arr.Length && (root.data == arr[index] &&  ` `                       ``(existPath(root.left,arr,index + 1) ||  ` `                        ``existPath(root.right, arr, index + 1)))); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``// arr[] is sequence of root to leaf path  ` `        ``int` `[]arr = {5, 8, 6, 7};  ` `        ``Node root=``new` `Node(5); ` `        ``root.left=``new` `Node(3); ` `        ``root.right=``new` `Node(8); ` `        ``root.left.left = ``new` `Node(2);  ` `        ``root.left.right = ``new` `Node(4);  ` `        ``root.left.left.left = ``new` `Node(1);  ` `        ``root.right.left = ``new` `Node(6);  ` `        ``root.right.left.right = ``new` `Node(7);  ` ` `  `        ``if``(existPath(root, arr, 0)) ` `        ``{ ` `            ``Console.Write(``"Path Exists"``); ` `        ``} ` `        ``else` `        ``{ ` `            ``Console.Write(``"Path does not Exist"``); ` `        ``} ` `    ``}  ` `} ` ` `  `/* A binary tree node has data,  ` `pointer to left child and a ` `pointer to right child */` `public` `class` `Node  ` `{  ` `    ``public` `int` `data;  ` `    ``public` `Node left, right;  ` `    ``public` `Node(``int` `data) ` `    ``{ ` `        ``this``.data = data; ` `        ``left = right = ``null``; ` `    ``} ` `}; ` ` `  `// This code is contributed Rajput-Ji  `

Output:

```Path Exists
```

Time complexity : O(n)

Tree Tree