# Root to leaf path with maximum distinct nodes

Given a Binary Tree, find count of distinct nodes in a root to leaf path with maximum distinct nodes.

**Examples:**

Input : 1 / \ 2 3 / \ / \ 4 5 6 3 \ \ 8 9 Output : 4 The root to leaf path with maximum distinct nodes is 1-3-6-8.

A **simple solution** is to explore all root to leaf paths. In every root to leaf path, count distinct nodes and finally return the maximum count.

An **efficient solution** is to use hashing. We recursively traverse the tree and maintain count of distinct nodes on path from root to current node. We recur for left and right subtrees and finally return maximum of two values.

Below is implementation of above idea

## C++

`// C++ program to find count of distinct nodes ` `// on a path with maximum distinct nodes. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A node of binary tree ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// A utility function to create a new Binary ` `// Tree node ` `Node* newNode(` `int` `data) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->data = data; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `temp; ` `} ` ` ` `int` `largestUinquePathUtil(Node* node, unordered_map<` `int` `, ` `int` `> m) ` `{ ` ` ` `if` `(!node) ` ` ` `return` `m.size(); ` ` ` ` ` `// put this node into hash ` ` ` `m[node->data]++; ` ` ` ` ` `int` `max_path = max(largestUinquePathUtil(node->left, m), ` ` ` `largestUinquePathUtil(node->right, m)); ` ` ` ` ` `// remove current node from path "hash" ` ` ` `m[node->data]--; ` ` ` ` ` `// if we reached a condition where all duplicate value ` ` ` `// of current node is deleted ` ` ` `if` `(m[node->data] == 0) ` ` ` `m.erase(node->data); ` ` ` ` ` `return` `max_path; ` `} ` ` ` `// A utility function to find long unique value path ` `int` `largestUinquePath(Node* node) ` `{ ` ` ` `if` `(!node) ` ` ` `return` `0; ` ` ` ` ` `// hash that store all node value ` ` ` `unordered_map<` `int` `, ` `int` `> hash; ` ` ` ` ` `// return max length unique value path ` ` ` `return` `largestUinquePathUtil(node, hash); ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `// Create binary tree shown in above figure ` ` ` `Node* root = newNode(1); ` ` ` `root->left = newNode(2); ` ` ` `root->right = newNode(3); ` ` ` `root->left->left = newNode(4); ` ` ` `root->left->right = newNode(5); ` ` ` `root->right->left = newNode(6); ` ` ` `root->right->right = newNode(7); ` ` ` `root->right->left->right = newNode(8); ` ` ` `root->right->right->right = newNode(9); ` ` ` ` ` `cout << largestUinquePath(root) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find count of

# distinct nodes on a path with

# maximum distinct nodes.

# A utility class to create a

# new Binary Tree node

class newNode:

def __init__(self, data):

self.data = data

self.left = self.right = None

def largestUinquePathUtil(node, m):

if (not node):

return len(m)

# put this node into hash

if node.data in m:

m[node.data] += 1

else:

m[node.data] = 1

max_path = max(largestUinquePathUtil(node.left, m),

largestUinquePathUtil(node.right, m))

# remove current node from path “hash”

m[node.data] -= 1

# if we reached a condition

# where all duplicate value

# of current node is deleted

if (m[node.data] == 0):

del m[node.data]

return max_path

# A utility function to find

# long unique value path

def largestUinquePath(node):

if (not node):

return 0

# hash that store all node value

Hash = {}

# return max length unique value path

return largestUinquePathUtil(node, Hash)

# Driver Code

if __name__ == ‘__main__’:

# Create binary tree shown

# in above figure

root = newNode(1)

root.left = newNode(2)

root.right = newNode(3)

root.left.left = newNode(4)

root.left.right = newNode(5)

root.right.left = newNode(6)

root.right.right = newNode(7)

root.right.left.right = newNode(8)

root.right.right.right = newNode(9)

print(largestUinquePath(root))

# This code is contributed by PranchalK

4

**Time Complexity :**O(n)

This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Sum of nodes on the longest path from root to leaf node
- Find the maximum sum leaf to root path in a Binary Tree
- Find if there is a pair in root to a leaf path with sum equals to root's data
- GCD from root to leaf path in an N-ary tree
- Check if there is a root to leaf path with given sequence
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Root to leaf path product equal to a given number
- Print the first shortest root to leaf path in a Binary Tree
- Remove nodes on root to leaf paths of length < K
- Print the path common to the two paths from the root to the two given nodes
- Print path from root to all nodes in a Complete Binary Tree
- Maximum number of leaf nodes that can be visited within the given budget
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.