Leetcode: Binary Tree Level Order Traversal: Two Queue
2013-01-08 00:59
441 查看
/** * Definition for binary tree * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > vv; if(!root) return vv; vv.clear(); vector<int> v; v.clear(); queue<TreeNode*> current,next; current.push(root); while(!current.empty()){ TreeNode* tmp=current.front(); current.pop(); v.push_back(tmp->val); if(tmp->left) next.push(tmp->left); if(tmp->right) next.push(tmp->right); if(current.empty()){ vv.push_back(v); v.clear(); swap(current,next); } } return vv; } };
相关文章推荐
- LeetCode(26)-Binary Tree Level Order Traversal II
- leetcode Binary Tree Level Order Traversal II
- LeetCode-binary-tree-level-order-traversal
- LeetCode-M-Binary Tree Level Order Traversal
- LeetCode(32)-Binary Tree Level Order Traversal
- LeetCode OJ Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal
- LeetCode 102 Binary Tree Level Order Traversal
- leetcode - Binary Tree Zigzag Level Order Traversal
- leetcode — binary-tree-level-order-traversal
- [Leetcode]Binary Tree Zigzag Level Order Traversal
- LeetCode - Binary Tree Zigzag Level Order Traversal
- [Leetcode] Binary Tree Level Order Traversal
- [Leetcode] Binary Tree Zigzag Level Order Traversal
- leetcode [Binary Tree Level Order Traversal II]
- Binary Tree Zigzag Level Order Traversal -- leetcode
- 【LeetCode】Binary Tree Level Order Traversal II 解题报告
- Leetcode: Binary Tree Zigzag Level Order Traversal
- [leetcode]Binary Tree Zigzag Level Order Traversal
- LeetCode: Binary Tree Level Order Traversal