面试题32:从上到下打印二叉树

题目:不分行从上到下打印二叉树。从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
思路:就是一个广度优先遍历,先把同一层的节点存储到队列中,然后按照顺序取出并且打印,接着把对应的左右子节点加入到队尾,依次进行。
解决方案:

public class Question32 {
    static class BinaryTreeNode{
        double value;
        BinaryTreeNode left;
        BinaryTreeNode right;
        public BinaryTreeNode(int value){
            this.value = value;
        }
    }
    public static void printFromTopToBottom(BinaryTreeNode treeRoot){
        if (treeRoot == null) return;
        Queue queue = new LinkedList<>();
        queue.add(treeRoot);
        while (!queue.isEmpty()){
            BinaryTreeNode tmpNode = queue.poll();
            System.out.println(tmpNode.value);
            if (tmpNode.left != null){
                queue.add(tmpNode.left);
            }
            if (tmpNode.right != null){
                queue.add(tmpNode.right);
            }
        }

    }

    public static void main(String[] args) {
        BinaryTreeNode pHead = new BinaryTreeNode(1);
        BinaryTreeNode pAhead = new BinaryTreeNode(3);
        BinaryTreeNode pBhead = new BinaryTreeNode(5);
        BinaryTreeNode pChead = new BinaryTreeNode(7);
        pHead.left = pAhead;
        pHead.right = pBhead;
        pBhead.left = pChead;
        printFromTopToBottom(pHead);
    }
}

题目:分行从上到下打印二叉树。从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
思路:在如上的基础上,加两个参数nextLevel和toBePrinted。nextLevel表示下一层的节点数,toBePrinted表示在当前层中还没有打印的节点数。
解决方案:

public class Question32 {
    static class BinaryTreeNode{
        double value;
        BinaryTreeNode left;
        BinaryTreeNode right;
        public BinaryTreeNode(int value){
            this.value = value;
        }
    }
   public static void print(BinaryTreeNode treeRoot){
        if (treeRoot == null) return;
        Queue queue = new LinkedList<>();
        queue.add(treeRoot);
        int nextLevel = 0;
        int toBePrinted = 1;
        while (!queue.isEmpty()){
            BinaryTreeNode tmpNode = queue.poll();
            System.out.print(tmpNode.value + " ");
            if (tmpNode.left != null){
                queue.add(tmpNode.left);
                ++nextLevel;
            }
            if (tmpNode.right != null){
                queue.add(tmpNode.right);
                ++nextLevel;
            }
            --toBePrinted;
            if (toBePrinted == 0){
                System.out.println("\t");
                toBePrinted = nextLevel;
                nextLevel = 0;
            }
        }
    }

    public static void main(String[] args) {
        BinaryTreeNode pHead = new BinaryTreeNode(1);
        BinaryTreeNode pAhead = new BinaryTreeNode(3);
        BinaryTreeNode pBhead = new BinaryTreeNode(5);
        BinaryTreeNode pChead = new BinaryTreeNode(7);
        pHead.left = pAhead;
        pHead.right = pBhead;
        pBhead.left = pChead;
        print(pHead);
    }
}

题目:之字形打印二叉树。实现一个函数按照之字形顺序打印二叉树,即第一层按照从左到右的顺序打印,第二层按照从右到左的顺序打印,其他行以此类推。
思路:构建两个栈,奇数层,先保存左子节点再保存右子节点;偶数层,则先保存右子节点再保存左子节点
解决方案:
public class Question32 {
static class BinaryTreeNode{
double value;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int value){
this.value = value;
}
}
public static void print3(BinaryTreeNode treeRoot) {
if (treeRoot == null) return;
Stack s1 = new Stack<>();
Stack s2 = new Stack<>();
s1.add(treeRoot);

    while (!s1.isEmpty() || !s2.isEmpty()) {
        if (!s1.isEmpty()){
            while (!s1.isEmpty()){
                BinaryTreeNode node = s1.pop();
                System.out.print(node.value + " ");
                if (node.left != null){
                    s2.add(node.left);
                }
                if (node.right != null){
                    s2.add(node.right);
                }
            }
            System.out.println();
        }else {
            while (!s2.isEmpty()){
                BinaryTreeNode node = s2.pop();
                System.out.print(node.value + " ");
                if (node.right != null){
                    s1.add(node.right);
                }
                if (node.left != null){
                    s1.add(node.left);
                }
            }
            System.out.println();
        }
    }
}

public static void main(String[] args) {
    BinaryTreeNode pHead = new BinaryTreeNode(1);
    BinaryTreeNode pAhead = new BinaryTreeNode(3);
    BinaryTreeNode pBhead = new BinaryTreeNode(5);
    BinaryTreeNode pChead = new BinaryTreeNode(7);
    pHead.left = pAhead;
    pHead.right = pBhead;
    pBhead.left = pChead;
    print3(pHead);
}

}

你可能感兴趣的:(面试题32:从上到下打印二叉树)