32 從上到下列印二叉樹

chasein發表於2020-09-23

題目一:不分行從上到下列印二叉樹

    public ArrayList<Integer> printTree(TreeNode root){
        ArrayList<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null){
            return res;
        }
        queue.add(root);
        while (!queue.isEmpty()){
            if (queue.peek().left != null){
                queue.add(queue.peek().left);
            }
            if (queue.peek().right != null){
                queue.add(queue.peek().right);
            }
            res.add(queue.poll().val);
        }
        return res;
    }

題目二:分行從上到下列印二叉樹

    public ArrayList<ArrayList<Integer>> printTree(TreeNode root){
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null){
            return res;
        }

        queue.add(root);
        int curLine = 1;
        int nextLine = 0;
        ArrayList<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()){
            if (queue.peek().left != null){
                queue.add(queue.peek().left);
                nextLine++;
            }
            if (queue.peek().right != null){
                queue.add(queue.peek().right);
                nextLine++;
            }
            list.add(queue.poll().val);
            curLine--;
            if (curLine == 0){
                curLine = nextLine;
                nextLine = 0;
                res.add(new ArrayList<>(list));
                list.clear();
            }
        }
        return res;
    }

題目三:之字形列印二叉樹

    public ArrayList<ArrayList<Integer>> printTree(TreeNode root){
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        Stack<TreeNode> stack0 = new Stack<>();
        Stack<TreeNode> stack1 = new Stack<>();
        if (root == null){
            return res;
        }
        int dir = 1; //0代表從左向右列印,1代表從右向左列印
        int curLine = 1;
        int nextLine = 0;
        stack0.push(root);
        ArrayList<Integer> list = new ArrayList<>();
        while (!stack0.isEmpty() || !stack1.isEmpty()){
            if (dir == 1){
                if (stack0.peek().left != null){
                    stack1.push(stack0.peek().left);
                    nextLine++;
                }
                if (stack0.peek().right != null){
                    stack1.push(stack0.peek().right);
                    nextLine++;
                }
            }else {
                if (stack1.peek().right != null){
                    stack0.push(stack1.peek().right);
                    nextLine++;
                }
                if (stack1.peek().left != null){
                    stack0.push(stack1.peek().left);
                    nextLine++;
                }
            }

            if (dir == 1){
                list.add(stack0.pop().val);
            }else {
                list.add(stack1.pop().val);
            }

            curLine--;
            if (curLine == 0){
                curLine = nextLine;
                nextLine = 0;
                dir = (dir == 0 ? 1 : 0);
                res.add(new ArrayList<>(list));
                list.clear();
            }
        }
        return res;
    }

相關文章