我新建的个人博客,欢迎访问:hmilzy.github.io
102. 二叉树的层序遍历 题目链接:二叉树的层序遍历
创建一个队列,保存存进去的结点,每次记录队列大小,来进行循环出结点,放下一层结点。
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 42 43 class Solution { public List<List<Integer>> levelOrder (TreeNode root) { List<List<Integer>> result = new ArrayList <>(); if (root == null ) { return result; } Deque<TreeNode> deque = new LinkedList <>(); deque.offer(root); while (!deque.isEmpty()) { int size = deque.size(); List<Integer> list = new ArrayList <>(); for (int i = 0 ; i < size; i++) { TreeNode cur = deque.poll(); list.add(cur.val); if (cur.left != null ) { deque.offer(cur.left); } if (cur.right != null ) { deque.offer(cur.right); } } result.add(list); } return result; } }
107.二叉树的层次遍历 II 题目链接:二叉树的层次遍历 II
要求是从底层往上存。 第一种思路就是按照原来的顺序,最后再把结果翻转即可 第二种思路就是仍然循环插入,但是每次直接把list插在头部就行了 在这里我想使用一下第二种方法,所以原先的List就用不了了,可以用Linkelist
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 42 43 class Solution { public List<List<Integer>> levelOrderBottom (TreeNode root) { LinkedList<List<Integer>> result = new LinkedList <>(); if (root == null ) { return result; } Deque<TreeNode> deque = new LinkedList <>(); deque.offer(root); while (!deque.isEmpty()) { int size = deque.size(); List<Integer> list = new ArrayList <>(); for (int i = 0 ; i < size; i++) { TreeNode cur = deque.poll(); list.add(cur.val); if (cur.left != null ) { deque.add(cur.left); } if (cur.right != null ) { deque.add(cur.right); } } result.addFirst(list); } return result; } }
199. 二叉树的右视图 题目链接:二叉树的右视图
当遍历到某一层的最后一个结点,就把它的值取出来放进result。 如何判断到了最后一个结点呢。就是当每一层循环到i == size -1 时。
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 42 43 class Solution { public List<Integer> rightSideView (TreeNode root) { List<Integer> result = new ArrayList <>(); Deque<TreeNode> deque = new LinkedList <>(); if (root == null ) { return result; } deque.offer(root); while (!deque.isEmpty()) { int size = deque.size(); for (int i = 0 ; i < size; i++) { TreeNode cur = deque.poll(); if (cur.left != null ) { deque.offer(cur.left); } if (cur.right != null ) { deque.offer(cur.right); } if (i == size - 1 ) { result.add(cur.val); } } } return result; } }
637. 二叉树的层平均值 题目链接:二叉树的层平均值
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 42 43 class Solution { public List<Double> averageOfLevels (TreeNode root) { List<Double> result = new ArrayList <>(); Deque<TreeNode> deque = new LinkedList <>(); if (root == null ) { return result; } deque.offer(root); while (!deque.isEmpty()) { int size = deque.size(); double sum = 0 ; for (int i = 0 ; i < size; i++) { TreeNode cur = deque.poll(); sum += cur.val; if (cur.left != null ) { deque.offer(cur.left); } if (cur.right != null ) { deque.offer(cur.right); } } result.add(sum / size); } return result; } }
429.N叉树的层序遍历 题目链接:N叉树的层序遍历
二叉树层序遍历,左右结点放进deque N叉数层序遍历,子节点集合list遍历一个一个放入deque
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 42 43 44 45 46 class Solution { public List<List<Integer>> levelOrder (Node root) { List<List<Integer>> result = new ArrayList <>(); Deque<Node> deque = new LinkedList <>(); if (root == null ) { return result; } deque.offer(root); while (!deque.isEmpty()) { int size = deque.size(); List<Integer> list = new ArrayList <>(); for (int i = 0 ; i < size; i++) { Node cur = deque.poll(); list.add(cur.val); if (cur.children != null ) { for (Node node : cur.children) { deque.add(node); } } } result.add(list); } return result; } }