Count diagonal paths from a node to a leaf consisting of same valued nodes
Input:5 / \ 6 5 \ \ 6 5
Diagonal 6 – 6 and 5 – 5 contains equal value.
Therefore, the required output is 2.
Input:5 / \ 6 5 \ \ 5 5
- Traverse the given binary tree in a diagonal order and store the starting node of each diagonal as the key and for each key, store all the values in that diagonal in a Hashset.
- After the traversal, find the number of keys having size of the set equal to 1 and print it as the answer.
Below is the implementation of the above approach.
Time Complexity: O(N)
Auxiliary Space: O(N)
My Personal Notes arrow_drop_up