题目:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / 9 20 / 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
链接: http://leetcode.com/problems/binary-tree-level-order-traversal/
题解:
层序遍历二叉树。使用BFS, 用一个Queue来辅助存储当前层的节点。
Time Complexity - O(n), Space Complexity - O(n) (最多一层节点数)
public class Solution { public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(root == null) return result; Queue<TreeNode> queue = new LinkedList<TreeNode>(); ArrayList<Integer> list = new ArrayList<Integer>(); queue.add(root); int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()){ TreeNode node = queue.poll(); curLevel --; list.add(node.val); if(node.left != null){ queue.offer(node.left); nextLevel ++; } if(node.right != null){ queue.offer(node.right); nextLevel ++; } if(curLevel == 0){ curLevel = nextLevel; nextLevel = 0; result.add(new ArrayList<Integer>(list)); list.clear(); } } return result; } }
Update:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if(root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); //BFS ArrayList<Integer> list = new ArrayList<>(); queue.add(root); //Initialize int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()) { TreeNode node = queue.poll(); list.add(node.val); curLevel --; if(node.left != null) { queue.offer(node.left); nextLevel++; } if(node.right != null) { queue.offer(node.right); nextLevel++; } if(curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList<Integer>(list)); list.clear(); } } return res; } }
二刷:
使用一个queue来帮助BFS层序遍历,有两个记录当前层节点和下一层节点数的变量curLevel以及nextLevel。
Java:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } Queue<TreeNode> q = new LinkedList<>(); q.offer(root); int curLevel = 1, nextLevel = 0; List<Integer> list = new ArrayList<>(); while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; list.add(node.val); if (node.left != null) { q.offer(node.left); nextLevel++; } if (node.right != null) { q.offer(node.right); nextLevel++; } if (curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList<Integer>(list)); list.clear(); } } return res; } }
三刷:
不要忘记把结果保存到结果集里,并且也要clear当前层。
Java:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } List<Integer> level = new ArrayList<>(); Queue<TreeNode> q = new LinkedList<>(); q.offer(root); int curLevel = 1, nextLevel = 0; while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; level.add(node.val); if (node.left != null) { q.offer(node.left); nextLevel++; } if (node.right != null) { q.offer(node.right); nextLevel++; } if (curLevel == 0) { curLevel = nextLevel; nextLevel = 0; res.add(new ArrayList<Integer>(level)); level.clear(); } } return res; } }
Update: 换种写法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; Queue<TreeNode> q = new LinkedList<>(); q.offer(root); int curLevel = 1; List<Integer> list = new ArrayList<>(); while (!q.isEmpty()) { TreeNode node = q.poll(); curLevel--; list.add(node.val); if (node.left != null) q.offer(node.left); if (node.right != null) q.offer(node.right); if (curLevel == 0) { curLevel = q.size(); res.add(new ArrayList<>(list)); list.clear(); } } return res; } }
Reference:
https://leetcode.com/discuss/21778/java-solution-using-dfs
https://leetcode.com/discuss/22533/java-solution-with-a-queue-used
https://leetcode.com/discuss/58454/java-queue-solution-beats-100%25