Wednesday, December 14, 2011

hasPathSum() in BT / Vertical sum / Min. sum path in BT

Given a binary tree and a sum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Return false if no such path can be found

Question 2:Print Paths
Print all paths from root to leaf

Question 3:Longest Possible Path in  Binary Tree
Longest possible path in a tree, you had to return the end leaf nodes.

Question 4:Path with Min sum
Print the path with minimum sum in a binary tree
http://www.geeksforgeeks.org/find-the-maximum-sum-path-in-a-binary-tree/

Question 5:

given a binary tree and a value. Print all those path which sum up to that value. path need not to start from root but can contain root.
for .e.g
_________________5___________________
__________6____________7_____________
_____1_________2
in this tree for sum 9 the path is 1,6,2
for sum 8 the path is 6,2
for sum 20 path is 2,6,5,7


Useful Link:
http://www.careercup.com/question?id=14710711

Root to leaf Path equal to a sum
subtract the node value from the sum when recurring down,and check to see if the sum is 0 when you run out of tree.


bool hasPathSum(struct node* node, int sum)
{
  if (node == NULL)
  {
     return (sum == 0);
  }
  else
  {
    bool ans = 0; 
    int subSum = sum - node->data;
    if ( subSum == 0 && node->left == NULL && node->right == NULL )
      return 1;
    if(node->left)
      ans = ans || hasPathSum(node->left, subSum);
    if(node->right)
      ans = ans || hasPathSum(node->right, subSum);
    return ans;
  }
}
Print all paths from root to leaf
void printPathsRecur(struct tree* root, int path[], int pathLen)
{
  if (root==NULL) return;
  path[pathLen] = root->data;
  pathLen++;
  if (root->left==NULL && root->right==NULL)
  {
    printArray(path, pathLen);
  }
  else
  {
    printPathsRecur(root->left, path, pathLen);
    printPathsRecur(root->right, path, pathLen);
  }
}
void printArray(int ints[], int len)
{
  int i;
  for (i=0; i<len; i++)
  {
    printf("%d ", ints[i]);
  }
  printf("\n");
}   

Print Path with Min sum

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode
{
    struct TreeNode *left, *right;
    int data;

}TreeNode;

typedef TreeNode * Tree;

/*
Two passes with constant space
*/
int minSum(Tree t, int toPrint) 
{
    if(t == NULL) 
        return 0;

    int lsum = minSum(t->left, 0);
    int rsum = minSum(t->right, 0);

    if(toPrint)
        printf("%d ", t->data);

    int sum = t->data;

    if(lsum <= rsum)
        sum += minSum(t->left, toPrint);
    else
        sum += minSum(t->right, toPrint);

    return sum;
    
}

TreeNode * makeTreeNode(int data)
{
    TreeNode *n = (TreeNode *)calloc(sizeof(TreeNode), 1);
    n->data = data;

    return n;
}

int main()
{
    Tree t = makeTreeNode(3);

    t->left = makeTreeNode(2);
    t->right = makeTreeNode(4);


    t->left->left = makeTreeNode(2);
    t->left->right = makeTreeNode(1);
    t->right->left = makeTreeNode(2);
    t->right->right = makeTreeNode(1);

    minSum(t, 1);
    printf("\n");

    getchar();
    return 0;
}

Longest Possible path:
http://www.careercup.com/question?id=14800913

2 comments:

  1. void Mirror(struct node *root)
    { struct node *tmp;
    tmp=(struct node *) malloc(sizeof(struct node));

    if (root==NULL)
    return;

    else
    {
    Mirror(root->left);
    Mirror(root->right);

    tmp=root->left;
    root->left=root->right;
    root->right=tmp;
    }
    }

    ReplyDelete
  2. http://rajeevprasanna.blogspot.in/2011/07/mirror-image-of-tree-in-iterative-way.html

    ReplyDelete