剑指offer c++
3.从你头到尾打印链表 /** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x),next(NULL) { * } * }; */ class Solution { public: vector vector vector if(!head) return v2; while(head){ v1.push_back(head->val); head = head->next; } for(vector v2.push_back(*iter); } return v2; } }; 重建二叉树: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),left(NULL),right(NULL) {} * }; */ class Solution { public: TreeNode* reConstructBinaryTree(vector if(pre.size()==0) return NULL; TreeNode* t=new TreeNode(pre[0]); int index = -1; for(int i=0;i if(vin[i]==pre[0]) index = i; } vector vector vector vector t->left = reConstructBinaryTree(v1,v2); t->right = reConstructBinaryTree(v3,v4); return t; } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |