#每日话题20160928# 笔试面试高频题目:二叉树的打印

今日话题

二叉树的按层打印与ZigZag打印

此题为高频面试笔试题,也是今晚牛课堂直播讲解的例题,晚上8点听亚马逊左神为你答疑解惑。
牛课堂链接 : http://www.nowcoder.com/live/11
手机端地址 : http://m.nowcoder.com/live/11

话题描述

二叉树一直是笔试面试中几乎是每次必考的题目,今日话题是看以下的题目,可以回复自己的思路,也可贴出自己的代码,牛友们一起讨论。明日会将左神给的代码贴上~

【题目】

给定一棵二叉树的头节点head,分别实现按层打印和ZigZag打印二叉树的函数。

【例子】

如果一颗二叉树头节点值为1;第二层为2,3;第三层为4,5,6,7。

按层打印时,输出格式必须如下:

Level 1 : 1

Level 2 : 2 3

Level 3 : 4 5 6 7

ZigZag打印时,输出格式必须如下:

Level 1 from left to right: 1

Level 2 from right to left: 3 2

Level 3 from left to right: 4 5 6 7


栏目介绍
每日话题,是牛客网推出的新栏目,每天推出一个热门话题供大家讨论交流。
如果大家有关心的话题,也可以在下方留言或者私信牛妹,争取能让你关心的话题上榜。
话题的范围包括但不限于笔经面经,技术交流,职业发展等等,只要你关心的都可以向牛妹建议提出来讨论。

全部评论
贴上微博网友的回复:
点赞 回复 分享
发布于 2016-09-28 14:59
package problems_2016_09_28; import java.util.Deque; import java.util.LinkedList; import java.util.Queue; public class Problem_09_PrintBinaryTreeByLevelAndZigZag { public static class Node { public int value; public Node left; public Node right; public Node(int data) { this.value = data; } } public static void printByLevel(Node head) { if (head == null) { return; } Queue<Node> queue = new LinkedList<Node>(); int level = 1; Node last = head; Node nLast = null; queue.offer(head); System.out.print("Level " + (level++) + " : "); while (!queue.isEmpty()) { head = queue.poll(); System.out.print(head.value + " "); if (head.left != null) { queue.offer(head.left); nLast = head.left; } if (head.right != null) { queue.offer(head.right); nLast = head.right; } if (head == last && !queue.isEmpty()) { System.out.print("\nLevel " + (level++) + " : "); last = nLast; } } System.out.println(); } public static void printByZigZag(Node head) { if (head == null) { return; } Deque<Node> dq = new LinkedList<Node>(); int level = 1; boolean lr = true; Node cur = head; Node last = head; Node nLast = null; dq.offerFirst(head); pringLevelAndOrientation(level++, lr); while (!dq.isEmpty()) { if (lr) { cur = dq.pollFirst(); if (cur.left != null) { nLast = nLast == null ? cur.left : nLast; dq.offerLast(cur.left); } if (cur.right != null) { nLast = nLast == null ? cur.right : nLast; dq.offerLast(cur.right); } } else { cur = dq.pollLast(); if (cur.right != null) { nLast = nLast == null ? cur.right : nLast; dq.offerFirst(cur.right); } if (cur.left != null) { nLast = nLast == null ? cur.left : nLast; dq.offerFirst(cur.left); } } System.out.print(cur.value + " "); if (cur == last && !dq.isEmpty()) { lr = !lr; last = nLast; nLast = null; System.out.println(); pringLevelAndOrientation(level++, lr); } } System.out.println(); } public static void pringLevelAndOrientation(int level, boolean lr) { System.out.print("Level " + level + " from "); System.out.print(lr ? "left to right: " : "right to left: "); } // for test -- print tree public static void printTree(Node head) { System.out.println("Binary Tree:"); printInOrder(head, 0, "H", 17); System.out.println(); } public static void printInOrder(Node head, int height, String to, int len) { if (head == null) { return; } printInOrder(head.right, height + 1, "v", len); String val = to + head.value + to; int lenM = val.length(); int lenL = (len - lenM) / 2; int lenR = len - lenM - lenL; val = getSpace(lenL) + val + getSpace(lenR); System.out.println(getSpace(height * len) + val); printInOrder(head.left, height + 1, "^", len); } public static String getSpace(int num) { String space = " "; StringBuffer buf = new StringBuffer(""); for (int i = 0; i < num; i++) { buf.append(space); } return buf.toString(); } public static void main(String[] args) { Node head = new Node(1); head.left = new Node(2); head.right = new Node(3); head.left.left = new Node(4); head.right.left = new Node(5); head.right.right = new Node(6); head.right.left.left = new Node(7); head.right.left.right = new Node(8); printTree(head); System.out.println("==============="); printByLevel(head); System.out.println("==============="); printByZigZag(head); } }
点赞 回复 分享
发布于 2016-10-08 17:32

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
感性的干饭人在线蹲牛友:🐮 应该是在嘉定这边叭,禾赛大楼挺好看的
点赞 评论 收藏
分享
点赞 11 评论
分享
牛客网
牛客企业服务