标签:init tps image 链接 题目 for def res ems
题目链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>> levelOrder(TreeNode* root) { 13 vector<vector<int> > result; 14 if(root == nullptr) return result; 15 queue<TreeNode*> current, next; 16 vector<int> level; // elments in level level 17 current.push(root); 18 while (!current.empty()) { 19 while (!current.empty()) { 20 TreeNode* node = current.front(); 21 current.pop(); 22 level.push_back(node->val); 23 if (node->left != nullptr) next.push(node->left); 24 if (node->right != nullptr) next.push(node->right); 25 } 26 result.push_back(level); 27 level.clear(); 28 swap(next, current); 29 } 30 return result; 31 } 32 };
标签:init tps image 链接 题目 for def res ems
原文地址:https://www.cnblogs.com/shixinzei/p/11667051.html