107. Binary Tree Level Order Traversal II
题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
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-ii/
题解:
这几道题都差不多,有关level order的。也可能有很好的方法,但是都没仔细想。基本思路和其他一样,就是BFS
Time Complexity - O(n), Space Complexity - O(1)。
/** * 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>> levelOrderBottom(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if(root == null) return res; ArrayList<Integer> list = new ArrayList<>(); Queue<TreeNode> q = new LinkedList<>(); q.offer(root); int curLevel = 1, nextLevel = 0; 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(0, new ArrayList<Integer>(list)); list.clear(); } } return res; } }
二刷 :
方法跟一刷一样,就是跟普通level order traversal一样,但是保存结果的时候点到以下顺序。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
/** * 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>> levelOrderBottom(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> level = new ArrayList<>(); 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(0, new ArrayList<Integer>(level)); level.clear(); } } return res; } }
题外话: 2/14/2016
时间过得真的很快,不抓紧的话根本刷不完。前面一段时间效率极低,每天一两题,也不愿意花时间刷题,反而停留在舒适区,看看书,看看设计。后来返回来刷题发现根本不熟。
最后祝大家情人节快乐。
三刷:
再看了一下discuss,好像用dfs始终比bfs快.
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>> levelOrderBottom(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(); level.add(node.val); curLevel--; 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(0, new ArrayList<Integer>(level)); level.clear(); } } return res; } }
Reference:
https://leetcode.com/discuss/5353/there-better-regular-level-order-traversal-reverse-result
https://leetcode.com/discuss/91100/share-java-2ms-recursive-solution
https://leetcode.com/discuss/22538/my-dfs-and-bfs-java-solution
https://leetcode.com/discuss/81189/java-1ms-beats-98%25-using-preorder