Sum of all odd nodes in the path connecting two given nodes
Given a binary tree and two nodes of that binary tree. Find the sum of all nodes with odd values in the path connecting the two given nodes.
For Example: In the above binary tree, sum of all odd nodes in the path between the nodes and will be 5 + 1 + 3 = 9.
Source : Amazon Interview Experience on Campus
Approach : The idea is to first find the path between the two given nodes of the binary tree using the concept as discussed in: Print path between any two nodes.
Once, we have the path between the two given nodes, calculate sum of all the odd valued nodes in that path and print it.
Below is the implementation of the above approach:
- Time Complexity : O(n)
- Auxiliary Space : O(n)
My Personal Notes arrow_drop_up
Please Login to comment...