Root to Leaf Paths | Practice | GeeksforGeeks

Given a Binary Tree of size N, you need to find all the possible paths from root node to all the leaf node's of the binary tree.
Example 1:
Input:
       1
    /     \
   2       3
Output: 1 2 #1 3 #
Explanation: 
All possible paths:
1->2
1->3


Example 2:
Input:
         10
       /    \
      20    30
     /  \
    40   60
Output: 10 20 40 #10 20 60 #10 30 #


Your Task:
Your task is to complete the function Paths() that takes the root node as an argument and return all the possible path. (All the path are printed '#' separated by the driver's code.)
Note: The return type
cpp: vector
java: ArrayList>
python: list of list
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree.
Constraints:
1<=N<=103
Note: The Input/Ouput format and Example given, are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
+1
keshavlad212 weeks ago
class Tree{
    ArrayList<ArrayList<Integer>>ans=new ArrayList<>();
    ArrayList<Integer>temp=new ArrayList<>();
    public ArrayList<ArrayList<Integer>> Paths(Node root){
         path(root);
         return ans;
    }
    public void path(Node root){
        if(root == null ){
            return ;
        }
        temp.add(root.data);
        if(root.left==null && root.right == null){
            ans.add(new ArrayList<Integer>(temp));
        }
        path(root.left);
        path(root.right);
        temp.remove(temp.size()-1); // while going back remove 			    		//last element from arrayList
        
    }
    
}
0
hk22578533 weeks ago
simple c++ soln
 void getpaths(Node* root, vector<int> &path, vector<vector<int>> &res)
 {
     if(root == NULL) return;
     path.push_back(root->data);
     if(!root->left && !root->right)
     {
         res.push_back(path);
     }
     
     getpaths(root->left, path, res);
     getpaths(root->right, path, res);
     path.pop_back();
 }
 
vector<vector<int>> Paths(Node* root)
{
    vector<int> path;
    vector<vector<int>> res;
    getpaths(root, path, res);
    return res;
}
0
amishasahu3281 month ago
void solve(Node *node, vector<vector<int>> &ans, vector<int> &v)
 {
     if(node == NULL)
        return;
     v.push_back(node->data);
     if(node->left == NULL && node->right == NULL)
     {
        ans.push_back(v);
        v.pop_back();
        return;
     }
     solve(node->left, ans, v);
     solve(node->right, ans, v);
     v.pop_back();
     return;
 }
vector<vector<int>> Paths(Node* root)
{
    // Code here
    vector<vector<int>> ans;
    vector<int> v;
    solve(root, ans, v);
    return ans;
}
0
moryasivam1 month ago
void bb(Node* root,vector<vector<int>>&v1,vector<int>v){   if(!root) return ;          v.push_back(root->data);         if(!root->left && !root->right){        v1.push_back(v);        v.pop_back();        return ;    }        bb(root->left,v1,v);    bb(root->right,v1,v);        return ;}
vector<vector<int>> Paths(Node* root){  vector<int>v;  vector<vector<int>>v1;  if(!root) return v1;  bb(root,v1,v);  return v1;}
0
ranjanchinmoy1 month ago
What is wrong with this code??? 
vector<vector<int>>ans;
 void dfs(Node* node,vector<int>curr)
 {
     if(node==NULL)
        return;
     curr.push_back(node->data);
     if(node->left==NULL && node->right==NULL)
     {
         ans.push_back(curr);
     }
     dfs(node->left,curr);
     dfs(node->right,curr);
     curr.pop_back();
 }
vector<vector<int>> Paths(Node* root)
{
    vector<int>curr;
    dfs(root,curr);
    return ans;
}
0
owaischem2 months ago
PYTHON SOLUTION:
TIME TAKEN:0.2/1.4
 
def Paths(root):   def traversal(root):       if root==None:           return None              stack.append(root.data)              if root.left:           traversal(root.left)       if root.left==None and root.right==None:           list1.append(stack.copy())       if root.right:           traversal(root.right)                                    stack.pop()      list1=[]   stack=[]   traversal(root)      return list1
+1
deepakjoshicscoder2 months ago
C++ SOLUTION:
void solve(Node* root , vector<vector<int>> &v1 , vector<int> &v ){   if(!root){       return;   }   v.push_back(root->data);   if(!root->left and !root->right){       v1.push_back(v);       v.pop_back();       return;   }   solve(root->left , v1 , v);   solve(root->right , v1 , v);   v.pop_back();   return;}vector<vector<int>> Paths(Node* root){   // Code here   vector<vector<int>> v1;   vector<int> v;   if(!root)   return v1;   solve(root , v1 , v);   return  v1;
}
+1
tarun2002ts0213 months ago
void solve(Node* root,vector<vector<int>>&v1,vector<int>&v){   if(root==NULL)   return;   v.push_back(root->data);   if(!root->left && !root->right)   {             v1.push_back(v);        v.pop_back();              return;   }  solve(root->left,v1,v); solve(root->right,v1,v); v.pop_back();    return;}vector<vector<int>> Paths(Node* root){   // Code here   vector<vector<int>>v1;   if(root==NULL)   return v1;   vector<int>v;   solve(root,v1,v);   return v1;}
-1
shiva10903 months ago
void printPaths(Node* root , vector<int> &v ,  vector<vector<int>>& ans)
{
    if(root == NULL)return;
    
    if(root->left == NULL && root->right == NULL)
    {
        ans.push_back(v);
        if(v.size() > 0)
        v.pop_back();
        return;
    }
    
    printPaths(root->left , v , ans);
    printPaths(root->right , v , ans);
    
    if(v.size() > 0)
    v.pop_back();
    
    return;
}
vector<vector<int>> Paths(Node* root)
{
    // Code here
    vector<vector<int>> ans;
    vector<int> v;
    
    printPaths(root , v , ans);
    return ans;
}

0
anirudh1587cse194 months ago
void put(Node*root,vector<vector<int>>&v,vector<int>&v1){   if(root==NULL)   {       return;   }   v1.push_back(root->data);   if(root->left==NULL&&root->right==NULL)   {       v.push_back(v1);       v1.pop_back();       return;   }   put(root->left,v,v1);   put(root->right,v,v1);   v1.pop_back();   return;      }vector<vector<int>> Paths(Node* root){   // Code here   vector<vector<int>>v;   vector<int>v1;   put(root,v,v1);   return v;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.