面试题23:从上往下打印二叉树(层序遍历) && 面试题25:二叉树中和为某一值的路径
2016-04-28 15:35
661 查看
/************************************************************************* > Created Time: Thu 28 Apr 2016 12:12:28 PM PKT ************************************************************************/ #include<iostream> using namespace std; #include <deque> #include <vector> struct BinaryTreeNode { int value; BinaryTreeNode* left; BinaryTreeNode* right; }; typedef struct BinaryTreeNode bt; //层序遍历 void PrintTreeRow(bt* root) { if(root==NULL){ return; } deque<bt*> que; que.push_back(root); while(!que.empty()){ bt* temp=que.front(); cout<<temp->value<<ends; if(temp->left){ que.push_back(temp->left); } if(temp->right){ que.push_back(temp->right); } que.pop_front(); } } BinaryTreeNode* create(int value,BinaryTreeNode* left=NULL,BinaryTreeNode* right=NULL){ BinaryTreeNode* node=new BinaryTreeNode; node->value=value; node->left=left; node->right=right; return node; } void PrintPath(const vector<bt*> &path) { for(int i=0;i<path.size();i++){ cout<<path[i]->value<<" "; } cout<<endl; } void PrintPath(bt* node,int sum,vector<bt*> &path) { if(node==NULL){ return; } if(node->value==sum){ if(node->left==NULL && node->right==NULL){ path.push_back(node); PrintPath(path); path.pop_back(); }else{ return; } }else if(node->value<sum){ path.push_back(node); PrintPath(node->left,sum-node->value,path); PrintPath(node->right,sum-node->value,path); path.pop_back(); }else{ return; } } //面试题25:二叉树中和为某一值的路径 void PrintPath(bt* root,int sum) { vector<bt*> path; bt* pnode=root; PrintPath(pnode,sum,path); } int main() { //bt* af=create(4); //bt* ag=create(7); bt* ae=create(7); bt* ad=create(4); bt* ab=create(5,ad,ae); bt* ac=create(12); bt* aa=create(10,ab,ac); //cout<<the tree is <<ends; PrintTreeRow(aa); cout<<endl; PrintPath(aa,22); return 0; }
fu za lian biao de fu zhi
/************************************************************************* > File Name: complex-list-clone.cpp > Author: liudong > Mail: liudonwho@126.com > Created Time: Thu 28 Apr 2016 02:03:09 PM PKT ************************************************************************/ #include<iostream> using namespace std; struct ComplexListNode { int value; ComplexListNode* next; ComplexListNode* sibling; }; typedef struct ComplexListNode list; void PrintList(list* root); ComplexListNode* Clone(list* phead) { if(phead==NULL){ return NULL; } //first step: clone next list* pnode=phead; list* next; while(pnode!=NULL){ list* pnewnode=new list; pnewnode->value=pnode->value; pnewnode->next=pnode->next; pnewnode->sibling=NULL; pnode->next=pnewnode; pnode=pnewnode->next; } // cout<<"clone to the last"<<endl; // PrintList(phead); // cout<<"done"<<endl; //second step: clone sibling pnode=phead; list* pnewnode;//=pnode->next; while(pnode!=NULL){ pnewnode=pnode->next; if(pnode->sibling!=NULL){ pnewnode->sibling=pnode->sibling->next; } pnode=pnewnode->next; //pnewnode=pnode->next; } // cout<<"clone sibling"<<endl; // PrintList(phead); // cout<<"done"<<endl; //third step: depart pnode=phead; list* pclonehead=phead->next; list* pclonenode=pclonehead; pnode->next=pclonenode->next; pnode=pnode->next; while(pnode!=NULL){ pclonenode->next=pnode->next; pclonenode=pclonenode->next; pnode->next=pclonenode->next; pnode=pnode->next; } return pclonehead; } list* create(int value,list* next=NULL,list* sibling=NULL) { list* node=new list; node->value=value; node->next=next; node->sibling=sibling; return node; } void PrintList(list* phead) { list* node=phead; while(node!=NULL){ cout<<node->value<<" "; if(node->sibling!=NULL){ cout<<node->sibling->value; } cout<<endl; node=node->next; } } int main(int argc,char** argv) { list* e=create(5); list* d=create(4,e); list* c=create(3,d); list* b=create(2,c); list* a=create(1,b); a->sibling=c; b->sibling=e; d->sibling=b; PrintList(a); list* newlist=Clone(a); PrintList(newlist); return 0; }
相关文章推荐
- 超全的iOS面试题汇总
- 【面试】剑指offer题目解析及tips速查(不断更新中)
- 面试经历-----摩根士丹利(实习岗)
- 链表面试题--从尾到头打印单链表
- Java---练习(面试题) :字符串截取(2-最终版)
- Java---练习(面试题) :字符串截取(2-最终版)
- Java程序员的成长路程
- mysql面试
- 一些经典面试题的思考与解析
- 程序员才能看得懂的内涵段子
- 程序员的搞笑日常生活
- 面试常见算法
- 如何理解代码程序员的辛酸日常工作
- 面试题9
- 安卓面试试题
- 面试问题
- 如今仍在工作的12名最“屌”的程序员
- 一名程序员菜鸟工作近一年的感悟
- 奋斗吧,程序员——第十九章 无可奈何花落去,似曾相识燕归来
- 美国一半程序员非科班出身,他们是怎么做到的