首页 > 学院 > 开发设计 > 正文

107. Binary Tree Level Order Traversal II

2019-11-10 18:38:18
字体:
来源:转载
供稿:网友

跟之前的题一样,直接转vector

/** * Definition for a binary tree node. * 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) { queue<TreeNode*>qu; vector<vector<int>>ve; if(root == NULL) return ve; vector<int>vec; int num = 1; qu.push(root); while(!qu.empty()){ TreeNode* t; vec.clear(); int nn = num; num = 0; for(int i = 1; i <= nn; ++ i){ t = qu.front(); qu.pop(); vec.push_back(t -> val); if(t -> left != NULL){ num++; qu.push(t -> left); } if(t -> right != NULL){ num++; qu.push(t -> right); } } ve.push_back(vec); } int n = ve.size(); for(int i = 0; i < ve.size() / 2; ++ i){ vec = ve[i]; ve[i] = ve[n - i - 1]; ve[n - i - 1] = vec; } return ve; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表