
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all the paths from root, with a specified sum in Binary tree in C++
In this problem, we are given a Binary tree and a sum S. And we have to find the path starting from root to any node of the tree, which gives the sum equal to the given sum.
Input
Sum = 14 Output : path : 4 10 4 3 7
To find the solution to this problem, we need to find the preorder traversal of the binary tree. And then find the path that adds up to the given sum.
Example
#include<bits/stdc++.h> using namespace std; struct Node{ int key; struct Node *left, *right; }; Node* insertNode(int key){ Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } void printPathsUtilSum(Node* curr_node, int sum, int sum_so_far, vector<int> &path){ if (curr_node == NULL) return; sum_so_far += curr_node->key; path.push_back(curr_node->key); if (sum_so_far == sum ){ for (int i=0; i<path.size(); i++) cout<<path[i]<<"\t"; cout<<endl; } if (curr_node->left != NULL) printPathsUtilSum(curr_node->left, sum, sum_so_far, path); if (curr_node->right != NULL) printPathsUtilSum(curr_node->right, sum, sum_so_far, path); path.pop_back(); } void pathWithSum(Node *root, int sum){ vector<int> path; printPathsUtilSum(root, sum, 0, path); } int main (){ Node *root = insertNode(4); root->left = insertNode(10); root->right = insertNode(3); root->right->left = insertNode(7); root->right->right = insertNode(1); root->left->left = insertNode(8); root->left->right = insertNode(6); int sum = 14; cout<<"Paths with the given sum are : "<<endl; pathWithSum(root, sum); return 0; }
Output
Paths with the given sum are −
4 10 4 3 7
- Related Articles
- Print all k-sum paths in a binary tree in C++
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Print all root to leaf paths with there relative positions in C++
- Program to print path from root to a given node in a binary tree using C++
- Print all full nodes in a Binary Tree in C++
- Pseudo-Palindromic Paths in a Binary Tree in C++
- Print all internal nodes of a Binary tree in C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Print all leaf nodes of a binary tree from right to left in C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Print all paths from a given source to a destination in C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- Print all nodes in a binary tree having K leaves in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++

Advertisements