剑指 Offer Java版

剑指Offer Java版 面试题32:从上到下打印二叉树

2019-07-20  本文已影响171人  孙强Jimmy

题目一:不分行从上到下打印二叉树

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

练习地址

https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701

参考答案

import java.util.ArrayList;
import java.util.LinkedList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        LinkedList<TreeNode> nodeList = new LinkedList<>();
        nodeList.add(root);
        while (!nodeList.isEmpty()) {
            TreeNode node = nodeList.removeFirst();
            result.add(node.val);
            if (node.left != null) {
                nodeList.add(node.left);
            }
            if (node.right != null) {
                nodeList.add(node.right);
            }
        }
        return result;
    }
}

复杂度分析

题目二:分行从上到下打印二叉树

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

练习地址

https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288

参考答案

import java.util.ArrayList;
import java.util.LinkedList;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if (pRoot == null) {
            return result;
        }
        LinkedList<TreeNode> nodeList = new LinkedList<>();
        nodeList.add(pRoot);
        // 下一层的节点数
        int nextLevel = 0;
        // 在当前层中还没有打印的节点数
        int toBePrinted = 1;
        ArrayList<Integer> levelList = new ArrayList<>();
        while (!nodeList.isEmpty()) {
            TreeNode node = nodeList.removeFirst();
            levelList.add(node.val);
            if (node.left != null) {
                nodeList.add(node.left);
                nextLevel++;
            }
            if (node.right != null) {
                nodeList.add(node.right);
                nextLevel++;
            }
            if (--toBePrinted == 0) {
                result.add(levelList);
                toBePrinted = nextLevel;
                nextLevel = 0;
                levelList = new ArrayList<>();
            }
        }
        return result;
    }
}

复杂度分析

题目三:之字形打印二叉树

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

练习地址

https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0

参考答案

import java.util.ArrayList;
import java.util.Stack;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if (pRoot == null) {
            return result;
        }
        Stack<TreeNode>[] stacks = new Stack[2];
        stacks[0] = new Stack<>();
        stacks[1] = new Stack<>();
        int current = 0, next = 1;
        stacks[0].push(pRoot);
        ArrayList<Integer> levelList = new ArrayList<>();
        while (!stacks[0].isEmpty() || !stacks[1].isEmpty()) {
            TreeNode node = stacks[current].pop();
            levelList.add(node.val);
            if (current == 0) {
                if (node.left != null) {
                    stacks[1].push(node.left);
                }
                if (node.right != null) {
                    stacks[1].push(node.right);
                }
            } else {
                if (node.right != null) {
                    stacks[0].push(node.right);
                }
                if (node.left != null) {
                    stacks[0].push(node.left);
                }
            }
            if (stacks[current].isEmpty()) {
                result.add(levelList);
                levelList = new ArrayList<>();
                current = 1 - current;
                next = 1 - next;
            }
        }
        return result;
    }
}

复杂度分析

👉剑指Offer Java版目录
👉剑指Offer Java版专题

上一篇 下一篇

猜你喜欢

热点阅读