/**
* 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:
int height(TreeNode* root){
if(!root) return 0;
else return max(height(root->right), height(root->left)) + 1;
}
void traverse(TreeNode*root, vector<vector<int> > & res, int count){
if(!root) return;
res[count].push_back(root->val);
count--;
traverse(root->left, res, count);
traverse(root->right, res, count);
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root) {
int count = height(root);
vector<vector<int> > res(count,vector<int>());
traverse(root,res, count - 1);
return res;
}
};
107. 二叉树的层次遍历 II
最新推荐文章于 2023-07-05 21:27:44 发布