* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { public: vector<int> inorderVec; voidinorderTraversal(TreeNode* root) {if(root==NULL){ return; }if(root->left){inorderTraversal(root->left); } inorderVec.push_back(root->val);if(root->right){...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { vector<int> vec; TreeNode *cur, *pre; cur = root; while(cur) { if(cur->left == NULL) { vec.push_back(cur->val); cur = cur->ri...
*/ class Solution { private: void traversal...(TreeNode* cur, vector& vec) { if(cur == NULL) return ; traversal(cur...->left, vec); traversal(cur->right, vec); vec.push_back(cur->val); } public:...vector postorderTraversal(TreeNode* root) { vector vec; traversal(root, vec...
return vec; cur.push(root); vector<int> vv; queue<TreeNode *> next; while(!cur.empty()) { TreeNode *t = cur.front(); cur.pop(); vv.push_back(t->val); if(t->left) next.push(t->left); if(t->right) next.push(t->right); if(cur.empty()) { vec.insert(vec.begin(),...
*/ class Solution { private: void traversal...(TreeNode* cur, vector& vec) { if(cur == NULL) return ; traversal(cur...->left, vec); traversal(cur->right, vec); vec.push_back(cur->val); } public:...vector postorderTraversal(TreeNode* root) { vector vec; traversal(root, vec...
vecs.push_back(vec); } return vecs; } // 记录每层的个数;curCount当前层个数,nextCount下一层个节点个数 vector<vector<int>> levelOrderBottom1(TreeNode* root) { int curCount = 0, nextCount = 0, level = 0; vector<vector<int>> ret; ...
TreeNode *cur = root;while(!s.empty() || cur !=NULL) {if(cur !=NULL) { s.push(cur); cur = cur ->left; }else{ cur = s.top(); s.pop(); res.push_back(cur ->val); cur = cur ->right; } }returnres; } };
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode *root) { vector<int> vec; pre(root, vec); return vec; } void pre(TreeNode *root, vector<int> &vec) { ...
*/ class Solution { private: void traversal...(TreeNode* cur, vector& vec) { if(cur == NULL) return ; traversal(cur...->left, vec); traversal(cur->right, vec); vec.push_back(cur->val); } public:...vector postorderTraversal(TreeNode* root) { vector vec; traversal(root, vec...
cur_vec.push_back(t->val); if (t->left) q.push(t->left); if (t->right) q.push(t->right); } } return result; } 递归的解决方案: class Solution { public: vector<vector<int>> result; void buildVector(TreeNode* root, int depth) ...