LeetCode_Binary Tree Level Order Traversal II
A过了,但是是那种BT解法,明天再想个正解
/** * 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> > levelOrderBottom(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int>> result, result1; vector<int> tempR; queue<TreeNode *> a,b,c; if(root == NULL) return result; TreeNode *current; a.push(root); while(!a.empty()){ tempR.clear(); while(!a.empty()) { current = a.front(); a.pop(); tempR.push_back(current->val); if(current->left) b.push(current->left); if(current->right) b.push(current->right); } result.push_back(tempR); c = a; a = b; b = c; } for(int i =result.size()-1;i>=0;i--) result1.push_back(result[i]); return result1; } };
--------------------------------------------------------------------天道酬勤!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步