Advertisement
Guest User

postorder

a guest
Dec 10th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4.  
  5. /**
  6. * @param tree
  7. * The input BinaryTree.
  8. * @return A list of all keys in the tree, in post-order.
  9. */
  10. public static List<Integer> postOrder(BinaryTree tree) {
  11. // TODO
  12. if(tree == null){
  13. return new ArrayList<>();
  14. }
  15. List<Integer> list = new ArrayList<>();
  16. checkLeft(list, tree);
  17. checkRight(list, tree);
  18. list.add(tree.getKey());
  19. return list;
  20.  
  21. }
  22. public static void checkLeft(List<Integer> list, BinaryTree tree ){
  23. if(tree.hasLeft()){
  24. list.addAll(postOrder(tree.getLeft()));
  25. }
  26. }
  27. public static void checkRight(List<Integer> list, BinaryTree tree ){
  28. if(tree.hasRight()){
  29. list.addAll(postOrder(tree.getRight()));
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement