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)
- 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
- Remove all nodes which don't lie in any path with sum>= k
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Sum of nodes on the longest path from root to leaf node
- Print the path common to the two paths from the root to the two given nodes
- Root to leaf path with maximum distinct nodes
- Print path from root to all nodes in a Complete Binary Tree
- Shortest path between two nodes in array like representation of binary tree
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Print common nodes on path from root (or common ancestors)
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Print levels with odd number of nodes and even number of nodes
- Subtree of all nodes in a tree using DFS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : andrew1234