Binary Tree Inorder Traversal

I简单 直接递归就好 addAll函数很好用
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList<Integer> order = new ArrayList<Integer>();
        if (root != null){
            order.addAll(inorderTraversal(root.left));
            order.add(root.val);
            order.addAll(inorderTraversal(root.right));
        }
        return order;
    }
}


额 II就比较麻烦了 还是递归 但是边界条件搞得不清楚。。。
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList<ArrayList<Integer>> order = new ArrayList<ArrayList<Integer>>();
        if (root != null){
            ArrayList<ArrayList<Integer>> leftOrder = levelOrderBottom(root.left);
            ArrayList<ArrayList<Integer>> rightOrder = levelOrderBottom(root.right);
            int leftdepth = leftOrder.size();
            int rightdepth = rightOrder.size();
            int depth = Math.max(leftdepth, rightdepth) + 1;
            ArrayList<Integer> temp;
            for (int i = 0; i < depth - 1; ++i){
                temp = new ArrayList<Integer>();
                if (i - (depth- 1 - leftdepth) >= 0)
                    temp.addAll(leftOrder.get(i - (depth- 1 - leftdepth)));
                if (i - (depth - 1 - rightdepth) >= 0)
                    temp.addAll(rightOrder.get(i - (depth- 1 - rightdepth)));
                order.add(temp);
            }
            temp = new ArrayList<Integer>();
            temp.add(root.val);
            order.add(temp);
        }
        return order;
    }
}

擦 small测试全对 big测试超时。。。日 然后= = 多测试几次 终于有一次accepted了!汗。。。不管了
第三个的递归有点难想,除了最后需要Collections类reverse每行一下,而且还要考虑左右节点如何拼接= =
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList<ArrayList<Integer>> order = new ArrayList<ArrayList<Integer>>();
        if (root != null){
            ArrayList<ArrayList<Integer>> leftOrder = zigzagLevelOrder(root.left);
            ArrayList<ArrayList<Integer>> rightOrder = zigzagLevelOrder(root.right);
            int leftdepth = leftOrder.size();
            int rightdepth = rightOrder.size();
            int depth = Math.max(leftdepth, rightdepth) + 1;
            ArrayList<Integer> temp = new ArrayList<Integer>();
            temp.add(root.val);
            order.add(temp);
            for (int i = 0; i < depth - 1; ++i){
                temp = new ArrayList<Integer>();
                if (i % 2 == 0) {
                    if (i < leftdepth)
                        temp.addAll(leftOrder.get(i));
                    if (i < rightdepth)
                        temp.addAll(rightOrder.get(i));
                } else {
                    if (i < rightdepth)
                        temp.addAll(rightOrder.get(i));
                    if (i < leftdepth)
                        temp.addAll(leftOrder.get(i));
                }
                Collections.reverse(temp);
                order.add(temp);
            }
        }
        return order;
    }
}

你可能感兴趣的:(binary)