题解 | #从下到上打印二叉树#
从下到上打印二叉树
http://www.nowcoder.com/practice/ed982e032ff04d6a857b4cb4e6369d04
/**
- struct TreeNode {
- int val;
- struct TreeNode *left;
- struct TreeNode *right;
- TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- };
/
class Solution {
public:
/*
-
代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
-
@param root TreeNode类
-
@return int整型vector<vector<>> / vector<vector > levelOrderBottom(TreeNode root) { vector<vector > vec; if(root == nullptr) return vec; queue<TreeNode*> q; q.emplace(root); while(!q.empty()) { vector vec1; int a = q.size(); for(int i = 0; i<a; i++) { TreeNode* tmp = q.front(); q.pop(); vec1.push_back(tmp->val); if(tmp->left) q.push(tmp->left); if(tmp->right) q.push(tmp->right);
} vec.push_back(vec1);} reverse(vec.begin(), vec.end()); return vec; // write code here } };
-