/*************************************************************************
> 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;
}