【Leetcode】Binary Tree Level Order Traversal II
来源:互联网 发布:单片机第二章答案详解 编辑:程序博客网 时间:2024/06/10 01:14
Very classic question and one of the hardest one I've met recently!!!
The solution is also very clever and impressive! If I can solve this problem during interview, I guess Google could be my next internship company without doubt!
Ok, here we go:
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]]
-----------------------------------------------------------------------------Explanation----------------------------------------------------------------------------
The problem is quite simple actually. Given a binary tree, we want to output according to the level. Therefore, the return type is such data structure as List<List<Integer>> where the internal list contains the integers in certain level and external list contains inner lists from bottom to top.
1) My first attempt is to do BFS and use Stack<Integer> to store those elements by using preorder traversal BFS. But the problem is after we done BFS, we cannot tell how many elements in each level.
2) Then I can't stop thinking maybe we can recursively add elements to its corresponding list recognized by its level, which is also the "tag" of its list!
Then I wrote my code like this:
//This is attributeprivate List<List<Integer>> list = new ArrayList<List<Integer>>();//this is main entry anyway public List<List<Integer>> levelOrderBottom(TreeNode root) { int length = getTreeLength(root); initList(length); order(root, length - 1); return list; } //Do remember init list firstly and put empty son-list in it. That's helpful private void initList(int length){ for(int i = 0; i < length; i++){ List<Integer> aList = new ArrayList<Integer>(); list.add(aList); } } //obtain the height of the tree, very clever! private int getTreeLength(TreeNode root){ if(root == null) return 0; if(root.left == null && root.right == null) return 1; return Math.max(getTreeLength(root.left) + 1, getTreeLength(root.right) + 1); } //very clever. when recursively traverse, just "get" corresponding level's aList from list to add more element. //Very easy to imagine and pretty accurate. Bravo! private void order(TreeNode root, int level){ if(root == null) return; list.get(level).add(root.val); if(root.left != null){ order(root.left, level - 1); } if(root.right != null){ order(root.right, level -1); } }
Very straightforward and easy to understand. Bravo done!
- LeetCode: Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [Leetcode] Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- 【leetcode】Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II
- [Leetcode]Binary Tree Level Order Traversal II
- [leetcode]Binary Tree Level Order Traversal II
- LeetCode-Binary Tree Level Order Traversal II
- LeetCode - Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- LeetCode | Binary Tree Level Order Traversal II
- LeetCode - Binary Tree Level Order Traversal II
- 【leetcode】Binary Tree Level Order Traversal II
- 【LeetCode】Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- 51Nod1119机器人走方格(费马儿定理+快速幂函数) 好题
- printd函数的编写(递归)
- 使用python建立数据层模块,致敬廖雪峰老师
- LeetCode_60 Permutation Sequence
- CF618D - Hamiltonian Spanning Tree
- 【Leetcode】Binary Tree Level Order Traversal II
- 51Nod 1181 质数中的质数(质数筛选好题)(数论)
- LeetCode_77 Combinations
- LeetCode_78 Subsets
- LeetCode_79 Word Search
- Leetcode bit manipulation 总结
- BigBench: Toward An Industry-Standard Benchmark for Big Data Analytics
- 注意OD仅以16进制解析指令立即数
- Leetcode 322. Coin Change