标签:treenode tor level 搜索 优先 tree tco -o 基础
problem:https://leetcode.com/problems/binary-tree-level-order-traversal
二刷此题,现在看起来是基础中的基础题了。
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(!root) return res; queue<TreeNode*> q; q.push(root); while(!q.empty()) { vector<int> layer; int size = q.size(); while(size--) { TreeNode* cur = q.front(); q.pop(); layer.push_back(cur->val); if(cur->left) q.push(cur->left); if(cur->right) q.push(cur->right); } res.push_back(layer); } return res; } };
[宽度优先搜索] leetcode 102 Binary Tree Level Order Traversal
标签:treenode tor level 搜索 优先 tree tco -o 基础
原文地址:https://www.cnblogs.com/fish1996/p/11312280.html