GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
578 views
                             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 

asked in Programming by Boss (7.7k points) 41 203 312 | 578 views
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
I am not getting any clue of how to check whether the given string is a tree or not ?
So basically it will be prefix evaluation rt?
that could be infix or postfix too. then add up all numbers. No need of parse tree for adding . rt?

1 Answer

+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

answered by (21 points) 1 2 4


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23386 Points

  2. Bikram

    17068 Points

  3. Habibkhan

    8158 Points

  4. srestha

    6286 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4344 Points

  9. sushmita

    3964 Points

  10. Rishi yadav

    3804 Points


Recent Badges

Ancestor Arijit 2
Good Answer pC
Revival pC
Reader Rajesh R
Reader #Rahul
Popular Question Arnabi
100 Club Rahul68
Popular Question Kanchan kumari
Old-Timer Santanu
Devoted Reader smsubham
27,316 questions
35,170 answers
84,073 comments
33,262 users