root to leaf path print
C++
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class node
{
public:
int data;
node* left;
node* right;
};
/* Prototypes for funtions needed in printPaths() */
void printPathsRecur(node* node, int path[], int pathLen);
void printArray(int ints[], int len);
/*Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(node* node)
{
int path[1000];
printPathsRecur(node, path, 0);
}
/* Recursive helper function -- given a node,
and an array containing the path from the root
node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(node* node, int path[], int pathLen)
{
if (node == NULL)
return;
/* append this node to the path array */
path[pathLen] = node->data;
pathLen++;
/* it's a leaf, so print the path that led to here */
if (node->left == NULL && node->right == NULL)
{
printArray(path, pathLen);
}
else
{
/* otherwise try both subtrees */
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}
/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
int i;
for (i = 0; i < len; i++)
{
cout << ints[i] << " ";
}
cout<<endl;
}
/* utility that allocates a new node with the
given data and NULL left and right pointers. */
node* newnode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
/* Driver code*/
int main()
{
/* Constructed binary tree is
10
/ \
8 2
/ \ /
3 5 2
*/
node *root = newnode(10);
root->left = newnode(8);
root->right = newnode(2);
root->left->left = newnode(3);
root->left->right = newnode(5);
root->right->left = newnode(2);
printPaths(root);
return 0;
}
// This code is contributed by rathbhupendra
Also in C++:
- Title
- pair in c++
- Category
- C++
- Title
- how do you add two random numbers in c++
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- All palindromic substrings
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- initialize vector of pointers c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- matrix transpose tiling
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- exponenciacion binaria
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++