Given an integer ‘K’ and a tree in string format.how to solve below problem ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

    Given an integer ‘K’ and a tree in string format.how to solve below problem ?

    Given an integer ‘K’ and a tree in string format. We have to print the sum of all elements at Kth level from root.

    0
    / \
    5 7
    / \ / \
    6 4 1 3
    \
    9



    Tree given in the form: (node value(left subtree)(right subtree))
    For tree given above: (0(5(6()())(4() (9()())))(7(1() ())(3()())))
    Input format: K Tree
    Output format: Sum
    For example, for given tree:
    Input: 2 (0(5(6()())(4() (9()())))(7(1() ())(3()())))
    Output: 14

    I am unable to get the approach of starting with this , how to implement it using stack ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is done using recursion.

    Have you ever written a recursive function?

    Comment

    • radha gogia
      New Member
      • Feb 2015
      • 56

      #3
      Code:
      #include<stdio.h>
      #include<string.h>
      int main()
      {int sum=0;
      char str[40];
      char *ptr;
      int k;
      printf("enter string");
      gets(str);
      printf("enter depth");
      scanf("%d",&k);
      ptr=str;
      int depth =-1;
      while(*ptr!='\0')
      {
      if(*ptr=='(')
      {
      depth++;
      if(depth==k)
      {
      sum=sum+*(++ptr)-'0';
      }
      }
      else if (*ptr==')')
      {
      depth--;
      }
      }
      printf("%d", sum);
      return 0;
      }

      Started with a depth of -1. Increment the depth when we find a (. Decrement the depth when we find a ). If I encounter a digit when depth is equal to K, then convert the digits to a number, and add the number to the sum.

      But this code isn't working ,please help

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Change the code so you don't enter the string for the tree.

        The string must be (0(5(6()())(4() (9()())))(7(1() ())(3()()))) exactly.

        Let me know what happens.

        Comment

        • radha gogia
          New Member
          • Feb 2015
          • 56

          #5
          I am unable to get your point , I entered this string only as input .

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The tree in the problem can only be this string:

            (0(5(6()())(4() (9()())))(7(1() ())(3()())))

            exactly.
            I have no idea what you entered but if it's not exactly this then your code won't work.

            By removing the data entry you remove one reason why the code doesn't work.

            After all, your program is just a test of your function to iterate the tree, it's not a full fledged app.

            Comment

            • radha gogia
              New Member
              • Feb 2015
              • 56

              #7
              I got the point , I forgot to increment the pointer , input was correct , thankss a lot

              Comment

              Working...