[leetcode] Binary Tree Level Order Traversal II
2014-07-20 02:05
363 查看
Binary Tree Level
Order Traversal II
解法:上题基础上反序即可。
Order Traversal II
解法:上题基础上反序即可。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: vector<vector<int>> res; public: vector<vector<int> > levelOrderBottom(TreeNode *root) { if (root==NULL) { return res; } queue<TreeNode*> curQ,nextQ; curQ.push(root); while (!curQ.empty()) { vector<int> layerRes; while (!curQ.empty()) { TreeNode *tmp=curQ.front(); curQ.pop(); layerRes.push_back(tmp->val); if (tmp->left!=NULL) {//使用!tmp->left出错 nextQ.push(tmp->left); } if (tmp->right!=NULL) { nextQ.push(tmp->right); } } curQ=nextQ; while (!nextQ.empty()) { nextQ.pop(); } res.push_back(layerRes); } reverse(res.begin(), res.end()); return res; } };
相关文章推荐
- 【Leetcode】之 Binary Tree Level Order Traversal II
- LeetCode: Binary Tree Level Order Traversal II [107]
- 【leetcode】Binary Tree Level Order Traversal II
- leetcode Binary Tree Level Order Traversal II
- [Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
- LeetCode-Binary Tree Level Order Traversal II
- leetcode---Binary Tree Level Order Traversal II
- 【LeetCode】Binary Tree Level Order Traversal II
- LeetCode[Tree]: Binary Tree Level Order Traversal II
- leetcode-107 Binary Tree Level Order Traversal II
- LeetCode – Refresh – Binary Tree Level Order Traversal ii
- [LeetCode] Binary Tree Level Order Traversal II
- leetcode---binary-tree-level-order-traversal-ii---树层次遍历
- 【LeetCode-107】Binary Tree Level Order Traversal II(C++)
- LeetCode Binary Tree Level Order Traversal II
- [LeetCode] 013: Binary Tree Level Order Traversal II
- [leetcode] Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II——LeetCode
- LeetCode #107 - Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II