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.

Binary Tree

For Example: In the above binary tree, sum of all odd nodes in the path between the nodes 5 and 6 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find sum of all odd nodes 
// in the path connecting two given nodes
  
#include<bits/stdc++.h>
using namespace std;
  
// Binary Tree node
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
  
// Utitlity function to create a 
// new Binary Tree node
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
      
    return node;
}
  
// Function to check if there is a path from root 
// to the given node. It also populates 
// 'arr' with the given path 
bool getPath(Node* root, vector<int>& arr, int x) 
    // if root is NULL 
    // there is no path 
    if (!root) 
        return false
  
    // push the node's value in 'arr' 
    arr.push_back(root->data); 
  
    // if it is the required node 
    // return true 
    if (root->data == x) 
        return true
  
    // else check whether the required node lies 
    // in the left subtree or right subtree of 
    // the current node 
    if (getPath(root->left, arr, x) || getPath(root->right, arr, x)) 
        return true
  
    // required node does not lie either in the 
    // left or right subtree of the current node 
    // Thus, remove current node's value from 
    // 'arr'and then return false 
    arr.pop_back(); 
    return false
  
// Function to get the sum of odd nodes in the 
// path between any two nodes in a binary tree 
int sumOddNodes(Node* root, int n1, int n2) 
    // vector to store the path of 
    // first node n1 from root 
    vector<int> path1; 
  
    // vector to store the path of 
    // second node n2 from root 
    vector<int> path2; 
  
    getPath(root, path1, n1); 
    getPath(root, path2, n2); 
  
    int intersection = -1; 
  
    // Get intersection point 
    int i = 0, j = 0; 
    while (i != path1.size() || j != path2.size()) { 
  
        // Keep moving forward until no intersection 
        // is found 
        if (i == j && path1[i] == path2[j]) { 
            i++; 
            j++; 
        
        else
            intersection = j - 1; 
            break
        
    
      
    int sum = 0;
      
    // claculate sum of ODD nodes from the path 
    for (int i = path1.size() - 1; i > intersection; i--) 
        if(path1[i]%2)
            sum += path1[i];
  
    for (int i = intersection; i < path2.size(); i++) 
        if(path2[i]%2)
            sum += path2[i];
              
    return sum;        
  
// Driver Code
int main()
{
    struct Node* root = newNode(1);
      
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->right = newNode(6);
      
    int node1 = 5; 
    int node2 = 6; 
      
    cout<<sumOddNodes(root, node1, node2); 
      
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find sum of all odd nodes 
// in the path connecting two given nodes 
  
import java.util.*;
class solution
{
  
// Binary Tree node 
static class Node 
    int data; 
     Node left; 
     Node right; 
  
// Utitlity function to create a 
// new Binary Tree node 
 static Node newNode(int data) 
     Node node = new Node(); 
    node.data = data; 
    node.left = null
    node.right = null
      
    return node; 
  
// Function to check if there is a path from root 
// to the given node. It also populates 
// 'arr' with the given path 
static boolean getPath(Node root, Vector<Integer> arr, int x) 
    // if root is null 
    // there is no path 
    if (root==null
        return false
  
    // push the node's value in 'arr' 
    arr.add(root.data); 
  
    // if it is the required node 
    // return true 
    if (root.data == x) 
        return true
  
    // else check whether the required node lies 
    // in the left subtree or right subtree of 
    // the current node 
    if (getPath(root.left, arr, x) || getPath(root.right, arr, x)) 
        return true
  
    // required node does not lie either in the 
    // left or right subtree of the current node 
    // Thus, remove current node's value from 
    // 'arr'and then return false 
    arr.remove(arr.size()-1); 
    return false
  
// Function to get the sum of odd nodes in the 
// path between any two nodes in a binary tree 
static int sumOddNodes(Node root, int n1, int n2) 
    // vector to store the path of 
    // first node n1 from root 
    Vector<Integer> path1= new Vector<Integer>(); 
  
    // vector to store the path of 
    // second node n2 from root 
    Vector<Integer> path2= new Vector<Integer>(); 
  
    getPath(root, path1, n1); 
    getPath(root, path2, n2); 
  
    int intersection = -1
  
    // Get intersection point 
    int i = 0, j = 0
    while (i != path1.size() || j != path2.size()) { 
  
        // Keep moving forward until no intersection 
        // is found 
        if (i == j && path1.get(i) == path2.get(j)) { 
            i++; 
            j++; 
        
        else
            intersection = j - 1
            break
        
    
      
    int sum = 0
      
    // claculate sum of ODD nodes from the path 
    for (i = path1.size() - 1; i > intersection; i--) 
        if(path1.get(i)%2!=0
            sum += path1.get(i); 
  
    for (i = intersection; i < path2.size(); i++) 
        if(path2.get(i)%2!=0
            sum += path2.get(i); 
              
    return sum;      
  
// Driver Code 
public static void main(String args[]) 
     Node root = newNode(1); 
      
    root.left = newNode(2); 
    root.right = newNode(3); 
    root.left.left = newNode(4); 
    root.left.right = newNode(5); 
    root.right.right = newNode(6); 
      
    int node1 = 5
    int node2 = 6
      
    System.out.print(sumOddNodes(root, node1, node2)); 
      
}

chevron_right



Output:

9

Time Complexity : O(n)
Auxiliary Space : O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : andrew1234



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.