Algorithm to print all paths with a given sum in a binary tree

后端 未结 18 1304
既然无缘
既然无缘 2020-12-24 07:04

The following is an interview question.

You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm t

相关标签:
18条回答
  • 2020-12-24 07:38

    Here is an approach with nlogn complexity.

    1. Traverse the tree with inorder.
    2. At the same time maintain all the nodes along with the cumulative sum in a Hashmap<CumulativeSum, reference to the corresponding node>.
    3. Now at a given node calculate cumulative sum from root to till the node say this be SUM.
    4. Now look for the value SUM-K in the HashMap.
    5. If the entry exists take the corresponding node reference in the HashMap.
    6. Now we have a valid path from the node reference to the current node.
    0 讨论(0)
  • 2020-12-24 07:41

    Based on Christian's answer above:

    public void printSums(Node n, int sum, int currentSum, String buffer) {
         if (n == null) {
             return;
         }
         int newSum = currentSum + n.val;
         String newBuffer = buffer + " " + n.val;
         if (newSum == sum) {
             System.out.println(newBuffer);
         }
         printSums(n.left, sum, newSum, newBuffer);
         printSums(n.right, sum, newSum, newBuffer);
         printSums(n.left, sum, 0, "");
         printSums(n.right, sum, 0, "");
    } 
    
    printSums(root, targetSum, 0, "");
    
    0 讨论(0)
  • 2020-12-24 07:41

    Since we need the paths having sum == k . I assume worst case complexity can be O(total_paths_in_tree) .

    So why not generate every path and check for the sum , anyways it is a tree having negative numbers and is not even a binary search tree .

        struct node{
          int val;
          node *left,*right;
    
          node(int vl)
          {
            val = vl;
            left = NULL;
            right = NULL;
          }
       };
    
    
       vector<vector<int> > all_paths;
       vector<vector<int> > gen_paths(node* root)
       {
           if(root==NULL)
           {
              return vector<vector<int> > ();
           }
    
           vector<vector<int> >    left_paths = gen_paths(root->left);
           vector<vector<int> >    right_paths = gen_paths(root->right);
    
           left_paths.push_back(vector<int> ()); //empty path
           right_paths.push_back(vector<int> ());
    
           vector<vector<int> > paths_here;
           paths_here.clear();
    
    
           for(int i=0;i<left_paths.size();i++)
           {
               for(int j=0;j<right_paths.size();j++)
               {
                  vector<int> vec;
                  vec.clear();
                  vec.insert(vec.end(), left_paths[i].begin(), left_paths[i].end());
                 vec.push_back(root->val);
                 vec.insert(vec.end(), right_paths[j].begin(), right_paths[j].end());
                 paths_here.push_back(vec);
               }
            }
    
            all_paths.insert(all_paths.end(),paths_here.begin(),paths_here.end());
    
           vector<vector<int> > paths_to_extend;
           paths_to_extend.clear();
    
           for(int i=0;i<left_paths.size();i++)
           {
                paths_to_extend.push_back(left_paths[i]);
                paths_to_extend[i].push_back(root->val);
           }
    
           for(int i=0;i<right_paths.size();i++)
           {
               paths_to_extend.push_back(right_paths[i]);
               paths_to_extend[paths_to_extend.size()-1].push_back(root->val);
           }
    
           return paths_to_extend;
        }
    

    For generating paths I have generated all left paths and all right paths And added the left_paths + node->val + right_paths to all_paths at each node. And have sent the paths which can still be extended .i.e all paths from both sides + node .

    0 讨论(0)
  • 2020-12-24 07:42

    https://codereview.stackexchange.com/questions/74957/find-all-the-paths-of-tree-that-add-to-a-input-value

    I have attempted an answer, expecting code review. My code as well as the reviewers should be helpful source.

    0 讨论(0)
  • 2020-12-24 07:43
    public void printPath(N n) {
        printPath(n,n.parent);
    }
    
    private void printPath(N n, N endN) {
        if (n == null)
            return;
        if (n.left == null && n.right == null) {
            do {
                System.out.print(n.value);
                System.out.print(" ");
            } while ((n = n.parent)!=endN);
            System.out.println("");
            return;
        }
        printPath(n.left, endN);
        printPath(n.right, endN);
    }
    

    You can print tree path end the n node. like this printPath(n);

    0 讨论(0)
  • 2020-12-24 07:44

    One can reduce this tree to a weighted graph G, where each edge weight = sum of values in each of its nodes.

    Then, run Floyd-Warshall algorithm on the graph G. By inspecting elements in the resulting matrix, we can get all pairs of nodes between which the total sum is equal to the desired sum.

    Also, note that the shortest path the algorithm gives is also the only path between 2 nodes in this tree.

    This is just another approach, not as efficient as a recursive approach.

    0 讨论(0)
提交回复
热议问题