Leetcode 102. Binary Tree Level Order Traversal 发表于 2019-10-23 | 分类于 Online Judge https://leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历 代码: 12345678910111213141516171819202122232425262728293031323334353637/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { queue<TreeNode*> q; q.push(root); vector<vector<int>> res; int num = 1; while(!q.empty()){ vector<int> arr; int count = 0; for(int i = 0; i < num; i++){ TreeNode* temp = q.front(); q.pop(); if(temp){ arr.push_back(temp->val); q.push(temp->left); q.push(temp->right); count += 2; } } if(!arr.empty()) res.push_back(arr); num = count; } return res; }}; 本文作者: Moon 本文链接: 2019/10/23/Leetcode-102/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!