剑指offer-day6
从上到下打印二叉树
经典题目,使用层序遍历来实现,具体实现方法就是用一个队列来记录节点,一个ArrayList记录答案。当前节点入队后,再将其左右节点入队,随后出队,ArrayList记录节点的值,直至队列为空。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Solution { public int[] levelOrder(TreeNode root) { LinkedList<TreeNode> q = new LinkedList<>(); ArrayList<Integer> res = new ArrayList<Integer>(); q.add(root); TreeNode t; while((t = q.peek()) != null){ if(t.left != null){ q.add(t.left); } if(t.right != null){ q.add(t.right); } res.add(q.poll().val); } int[] arr = new int[res.size()]; for(int i = 0; i<res.size(); i++){ arr[i] = res.get(i); } return arr; } }
|
从上到下打印二叉树Ⅱ
上一题的变种,加入了对当前层数的判断,需要在之前的代码上做一些修改,主要是用一个int值记录当前层数中的节点数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); Queue<TreeNode> q = new LinkedList<>(); if(root != null){ q.add(root); } while(q.size() != 0){ int i = q.size(); List<Integer> tmp = new ArrayList<>(); while(i > 0){ TreeNode t = q.poll(); tmp.add(t.val); if(t.left != null){ q.add(t.left); } if(t.right != null){ q.add(t.right); } i--; } res.add(tmp); } return res; } }
|
从上到下打印二叉树Ⅲ
究极加强版,在上一题的基础上加上了蛇形遍历,其实就是需要对层数的奇偶进行判断,而且需要把临时队列tmp更改为双端队列,当层数为奇时从尾部插入,反之从头部插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); Queue<TreeNode> q = new LinkedList<>(); if(root != null){ q.add(root); } while(q.size() != 0){ int i = q.size(); LinkedList<Integer> tmp = new LinkedList<>(); while(i > 0){ TreeNode t = q.poll(); if(res.size() % 2 == 0){ tmp.addLast(t.val); } else{ tmp.addFirst(t.val); } if(t.left != null){ q.add(t.left); } if(t.right != null){ q.add(t.right); } i--; } res.add(tmp); } return res; } }
|