0

Printing paths in a Binary Tree from root to leaves but the paths are not printing,

Paths in a Binary Search Tree from root to leaves

          1
       /     \
     2        3
   /   \     /  \
  4     5   6    7
        /
       8

.why is this problem coming please try to give me the solution.

#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

bool flag = true;

struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};

Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;

    return(node);
}

list<string> getPath(Node *root, list<string> l, string s)
{
    // Base Case
    if (root==NULL)
        return l;

       if(root->left == NULL && root->right== NULL) {
            if(!flag) {
                 s=s+"->";
            }
             s=s + to_string(root->data);
            l.push_back(s);
        }
        else {
            if(!flag) {
            s=s+"->";
            }
         s=s + to_string(root->data);
        }

        flag = false;
        if(root->left != NULL) {
            getPath (root->left,l,s);
        }

        if(root->right != NULL) {
            getPath (root->right,l,s);
        }

       return l;
}

list<string> binaryTreePaths(Node * root)
{
    string s="";
    list<string> l;
    return getPath(root, l, s);
}

//function for printing the elements in a list
void showlist(list <string> g)
{
    list <string> :: iterator it;
    for(it = g.begin(); it != g.end(); ++it)
        cout << '\t' << *it;
    cout << '\n';
}

int main()
{
    Node *root = newNode(1);
    root->left = newNode(2);
    root->right  = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->left->right = newNode(8);

    printf("Paths of this Binary Tree are:\n");
    list<string> s=binaryTreePaths(root);

    showlist(s);

    getchar();
    return 0;
}

Printing paths in a Binary Tree from root to leaves but the paths are not printing, why is this problem?

rbr94
  • 2,227
  • 3
  • 23
  • 39
Jonas
  • 43
  • 2
  • Did you try to debug it? I would give a look into your recursive call to getpath() – Federico Sep 02 '17 at 09:08
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Stop using it! – tambre Sep 02 '17 at 09:21
  • Don't include, it's a private non standard header not meant for inclusion. Why do you include C header? –  Sep 02 '17 at 09:39
  • Then how should I use it? @tambre – Jonas Sep 02 '17 at 13:49

1 Answers1

1

There is a very fundamental truth in C++ that arguments are passed by value, and modifying arguments inside the function will not modify them outside the function scope. If you want to modify l and s during the recursion, you would need to declare them as references, denoted by & in C++. Therefore the only change you would need to make in order to make the program output something is by declaring l as references.

list<string> getPath(Node *root, list<string>& l, string s)

Output: Paths of this Binary Tree are: 1->2->4->8 1->2->5 1->3->6 1->3->7

leyanpan
  • 433
  • 2
  • 9
  • Yes I missed the concept of references, thanks for helping. Actually I am expecting 1->2->4->8 , 1->2->5 , 1->3->6 , 1->3->7 . – Jonas Sep 02 '17 at 13:42
  • When running the same in my local code Blocks I am getting an error in to_string fun() why is this happening? @leyanpan – Jonas Sep 02 '17 at 13:44
  • What is the error? It works on my Visual Studio, though this might not be a very representative compiler. – leyanpan Sep 02 '17 at 15:32
  • Its is showing to_string() cannot be identified @leyanpan – Jonas Sep 03 '17 at 02:12
  • This is actually a question from https://leetcode.com/problems/binary-tree-paths/description/ – Jonas Sep 03 '17 at 02:13
  • You can simply use the itoa function in C. to_string is a new feature in C++11 so it might not be supported everywhere and you need to add a -std=C++11 when compiling with g++. – leyanpan Sep 03 '17 at 02:17
  • Though the best way is to use the stringstream in C++. – leyanpan Sep 03 '17 at 02:21
  • I've edited the answer. To achieve what you expect, you only need to declare the list as reference. – leyanpan Sep 03 '17 at 02:25