* 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...
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; } };
*/ 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...
// DO NOT write int main() function vector<vector<int> > vec; queue<TreeNode *> cur; if(root==NULL) 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)...
vector<int> vec; TreeNode* temp; for (int i = 0; i < size;i++) { temp = que.front(); que.pop(); vec.push_back(temp->val); if (temp->left) { que.push(temp->left); } if (temp->right) { que.push(temp->right); ...
* 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); ...
* 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) { ...
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) ...
NodeInfo cur_node=q.front(); q.pop();intcur_level =cur_node.level;intcur_node_val = cur_node.ptr->val;if(res.size()<cur_level+1) { vector<int>tempVec; res.push_back(tempVec); } res[cur_level].push_back(cur_node_val);if(cur_node.ptr->left) ...
return res;48}49};5051voidprint_vec(constvector<int>&vec){52for(inte:vec)53cout<<e<<"";54cout<<endl;55}5657intmain(){58TreeNode* root =newTreeNode(1);59root->right =newTreeNode(2);60root->right->left =newTreeNode(3);61vector<int> res =Solution().preorderTraversal(root);62...