LeetCode 102. Binary Tree Level Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-level-order-traversal/
题目:
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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
题解:
是一道BFS,利用queue,先进先出,并利用curCount来判断是否走完了当前一层。当curCount = 0 时表示当前一层走完,要把当前list加入res里,再刷新list,问题是不要忘记同时刷新curCount和nextCount.
Time Complexity: O(n). Space: O(n).
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode() {} 8 * TreeNode(int val) { this.val = val; } 9 * TreeNode(int val, TreeNode left, TreeNode right) { 10 * this.val = val; 11 * this.left = left; 12 * this.right = right; 13 * } 14 * } 15 */ 16 class Solution { 17 public List<List<Integer>> levelOrder(TreeNode root) { 18 List<List<Integer>> res = new ArrayList<>(); 19 if(root == null){ 20 return res; 21 } 22 23 LinkedList<TreeNode> que = new LinkedList<>(); 24 que.add(root); 25 26 while(!que.isEmpty()){ 27 int size = que.size(); 28 List<Integer> item = new ArrayList<>(); 29 while(size-- > 0){ 30 TreeNode cur = que.poll(); 31 item.add(cur.val); 32 if(cur.left != null){ 33 que.add(cur.left); 34 } 35 36 if(cur.right != null){ 37 que.add(cur.right); 38 } 39 } 40 41 res.add(item); 42 } 43 44 return res; 45 } 46 }
AC C++:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 vector<vector<int>> levelOrder(TreeNode* root) { 15 if(!root) { 16 return {}; 17 } 18 19 vector<vector<int>> res; 20 queue<TreeNode*> que; 21 que.push(root); 22 23 while(!que.empty()){ 24 int size = que.size(); 25 vector<int> curLevel; 26 27 for(int i = 0; i < size; i++){ 28 TreeNode* curNode = que.front(); 29 que.pop(); 30 curLevel.push_back(curNode->val); 31 32 if(curNode->left){ 33 que.push(curNode->left); 34 } 35 36 if(curNode->right){ 37 que.push(curNode->right); 38 } 39 } 40 41 res.push_back(curLevel); 42 } 43 44 return res; 45 } 46 };
AC Python:
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: 9 if not root: 10 return [] 11 12 res = [] 13 que = deque([root]) 14 15 while que: 16 curLen = len(que) 17 curLevel = [] 18 19 for _ in range(curLen): 20 curNode = que.popleft() 21 curLevel.append(curNode.val) 22 if curNode.left: 23 que.append(curNode.left) 24 25 if curNode.right: 26 que.append(curNode.right) 27 28 res.append(curLevel) 29 30 return res
类似的有Binary Tree Level Order Traversal II, Binary Tree Zigzag Level Order Traversal, Binary Tree Vertical Order Traversal, Binary Tree Right Side View, Find Largest Value in Each Tree Row, Check Completeness of a Binary Tree, Average of Levels in Binary Tree, N-ary Tree Level Order Traversal, Deepest Leaves Sum.