7.15 第一次做,无法 AC
7.16 一次 AC
题目描述
原题链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/
解题思路
K 神题解:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/
- 我能说啥呢,佩服。
7.16 思路:
关键就是当前的队列里面有多少值就遍历多少,下面标注的核心就是本题的核心。
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();Queue<TreeNode> queue = new LinkedList<>();if(root != null) queue.add(root);while(!queue.isEmpty()) {List<Integer> ans = new ArrayList<>();for(int i = queue.size(); i > 0; i--) { // 这一行代码核心TreeNode tmp = queue.poll();if(tmp.left != null) queue.add(tmp.left);if(tmp.right != null) queue.add(tmp.right);ans.add(tmp.val);}res.add(ans);}return res;}}
