in Programming in C
1,187 views
1 vote
1 vote
                             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 just want to know the basic logic using push and pop operation of stack ,since I think it is easy to work with stack here 

in Programming in C
1.2k views

4 Comments

why u want to do it by push and pop?

Better to write a simple binary tree and if node exists for the tree call sum function
0
0
I am not getting any clue of how to check whether the given string is a tree or not ?
0
0
So basically it will be prefix evaluation rt?
2
2
that could be infix or postfix too. then add up all numbers. No need of parse tree for adding . rt?
0
0

1 Answer

1 vote
1 vote

This problem can be solved easily with recursion on the basis of parentheses count, Have a look at my code and explanation :
sum of all elements at Kth level from root of a tree, represented as string

Related questions