标签:
二叉树的层次遍历
代码:
1 vector<vector<int> > levelOrder(TreeNode *root) { 2 vector<vector<int> > res; 3 queue<TreeNode *> layer; 4 5 layer.push(root); 6 while (!layer.empty()) { 7 queue<TreeNode *> nextLayer; 8 vector<int> path; 9 10 while (!layer.empty()) { 11 TreeNode *front = layer.front(); 12 layer.pop(); 13 if (!front) 14 continue; 15 path.push_back(front->val); 16 nextLayer.push(front->left); 17 nextLayer.push(front->right); 18 } 19 if (path.size() > 0) 20 res.push_back(path); 21 layer = nextLayer; 22 } 23 24 return res; 25 }
Leetcode#102 Binary Tree Level Order Traversal
标签:
原文地址:http://www.cnblogs.com/boring09/p/4267247.html