473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 ?
Jul 8 '16 #1
6 1713
weaknessforcats
9,208 Expert Mod 8TB
This is done using recursion.

Have you ever written a recursive function?
Jul 8 '16 #2
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {int sum=0;
  5. char str[40];
  6. char *ptr;
  7. int k;
  8. printf("enter string");
  9. gets(str);
  10. printf("enter depth");
  11. scanf("%d",&k);
  12. ptr=str;
  13. int depth =-1;
  14. while(*ptr!='\0')
  15. {
  16. if(*ptr=='(')
  17. {
  18. depth++;
  19. if(depth==k)
  20. {
  21. sum=sum+*(++ptr)-'0';
  22. }
  23. }
  24. else if (*ptr==')')
  25. {
  26. depth--;
  27. }
  28. }
  29. printf("%d", sum);
  30. return 0;
  31. }
  32.  

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
Jul 9 '16 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 9 '16 #4
I am unable to get your point , I entered this string only as input .
Jul 9 '16 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 9 '16 #6
I got the point , I forgot to increment the pointer , input was correct , thankss a lot
Jul 10 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Roger Helliwell | last post by:
Hello Everyone, Has anyone found a quick reference for the many string formatting options for string.Format() ? I'm spending a ridiculous amount of time trying to write one line of code that...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
11
by: Matt | last post by:
string format formula to write a text file below is not working for me pos 1 to 10 name pos 15 to 30 lastname pos 45 to 75 job Adam smith programmer string fmt = "{1,10}{15-30}{45,75}"...
2
by: Dilbert | last post by:
This is the code snippet: .... int nTest = 10; String * pMsg = String::Format( "Display an integer here: {0}", nTest); .... I got the following error message when tried to compile the program:...
1
by: Chris Morse | last post by:
Hi, I've been trying to figure out where in the documentation it describes all the String.Format() formatting specifiers. So far, I've been guessing and picking up specifiers in sample code.....
1
by: Agnes | last post by:
dim strPeriod as string = dtTransdate.ToString("dd-MMM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo) '--excel code ..Range("H2").Value = strPeriod When I read the excel, the...
4
by: Bishman | last post by:
Hi, A quick one for someone hopefully..... How do I return a string "003" from the integer 3 ? I believe its something like: int tempval = 3; string tempstr = ...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.