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.
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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Find triplet such that number of nodes connecting these triplets is maximum
- Largest component size in a graph formed by connecting non-co-prime nodes
- Diameters for each node of Tree after connecting it with given disconnected component
- Print the path common to the two paths from the root to the two given nodes
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Remove all nodes which don't lie in any path with sum>= k
- Count of all prime weight nodes between given nodes in the given Tree
- Count the nodes in the given tree whose sum of digits of weight is odd
- Print levels with odd number of nodes and even number of nodes
- Shortest path between two nodes in array like representation of binary tree
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Queries to check if the path between two nodes in a tree is a palindrome
- Print path between any two nodes in a Binary Tree | Set 2
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Check if two nodes are on same path in a tree | Set 2
- Print path from root to all nodes in a Complete Binary Tree
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U