Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Solution {
- /**
- * @param tree
- * The input BinaryTree.
- * @return A list of all keys in the tree, in post-order.
- */
- public static List<Integer> postOrder(BinaryTree tree) {
- // TODO
- if(tree == null){
- return new ArrayList<>();
- }
- List<Integer> list = new ArrayList<>();
- checkLeft(list, tree);
- checkRight(list, tree);
- list.add(tree.getKey());
- return list;
- }
- public static void checkLeft(List<Integer> list, BinaryTree tree ){
- if(tree.hasLeft()){
- list.addAll(postOrder(tree.getLeft()));
- }
- }
- public static void checkRight(List<Integer> list, BinaryTree tree ){
- if(tree.hasRight()){
- list.addAll(postOrder(tree.getRight()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement