Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int iterativeSum(Tree<Integer> t) {
- if (t == null)
- return 0;
- int sum = 0;
- Stack<Tree<Integer>> s = new Stack<Tree<Integer>>();
- s.push(t);
- Tree<Integer> tr;
- Set<Tree<Integer>> subtrees;
- while (!s.isEmpty()) {
- tr = s.pop();
- sum += tr.elem();
- subtrees = tr.subtrees();
- for (Tree<Integer> subtree : subtrees) {
- s.push(subtree);
- }
- }
- return sum;
- }
Add Comment
Please, Sign In to add comment