//Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).
//
// 译文:
//
// 给定一棵二叉查找树,设计算法,将每一层的所有结点构建为一个链表(也就是说, 如果树有D层,那么你将构建出D个链表) #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <list>
using namespace std; struct treenode
{ char data;
treenode * left;
treenode * right; }; class tree
{
public: tree()
{
//root = create();
root = NULL;
index = 0;
} /***输入扩展层次遍历序列,#表示该节点为空***/
tree(char *s)
{
root = NULL;
index = 0;
if (s == NULL)
{
return;
} int size = strlen(s);
create(&root,s,0,size);
} ~tree()
{
/***清空二叉树***/
} /***二叉排序树:插入。***/
void insert(char *s)
{
if (s == NULL)
{
return;
} int size = strlen(s);
for (int i = 0; i<size; i++)
{
insert(&root, s[i]);
}
} void createBanlance(char *s)
{
int size = strlen(s); createB(&root, s, 0, size-1);
cout<<"End"<<endl;
} void levelOrder()
{
queue<treenode *> q;
if (root != NULL)
{
q.push(root);
}
while(!q.empty())
{
treenode *t = q.front();
cout<<t->data<<endl;
q.pop();
if (t->left != NULL)
{
q.push(t->left);
}
if (t->right != NULL)
{
q.push(t->right);
}
}
}
/***
方法同hawstein
***/
vector<list<treenode *>> createList()
{
vector<list<treenode *>> l;
int level = 0;
list<treenode *>li;
li.push_back(root);
l.push_back(li); while(!l[level].empty())
{
list<treenode *>t; for (list<treenode *>::iterator it = l[level].begin(); it != l[level].end(); it++)
{
treenode *n = *it;
if (n->left){t.push_back(n->left);}
if (n->right){t.push_back(n->right);}
}
level++;
l.push_back(t);
}
return l;
} /***
使用一个栈、两个队列来实现
***/
vector<list<treenode *>> createlist()
{
vector<list<treenode *>>l;
int i = 0;
stack<treenode *>st;
queue<treenode *>qa,qb;
st.push(root); while(!st.empty())
{
while(!st.empty())
{
treenode *n = new treenode;
n->data = st.top()->data;
n->left = st.top()->left;
n->right = st.top()->right; qa.push(n);
qb.push(n);
st.pop();
}
//i++;
list<treenode *>li;
while(!qa.empty())
{
treenode *t = new treenode;
t->data = qa.front()->data;
t->left = qa.front()->left;
t->right = qa.front()->right; // l[i].push_back(t);
li.push_back(t);
qa.pop();
}
l.push_back(li); while(!qb.empty())
{
if (qb.front()->left)
{
treenode *t = new treenode;
t->data = qb.front()->left->data;
t->left = qb.front()->left->left;
t->right = qb.front()->left->right;
st.push(t);
} if (qb.front()->right)
{
treenode *tb = new treenode;
tb->data = qb.front()->right->data;
tb->left = qb.front()->right->left;
tb->right = qb.front()->right->right;
st.push(tb);
} qb.pop();
} }
return l;
} bool isBanlance(int size)
{
int *s = new int[size];
depth(root,s,size,0);
s[index+1] = '\0';
int i = 0;
int max = s[0];
int min = s[0];
while(s[i]!='\0')
{
if (s[i]>max)
{
max = s[i];
}
if (s[i]<min)
{
min = s[i];
}
i++;
}
return ((max - min)>1? 0:1);
} void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);} private: treenode *root;
int index; void insert(treenode **p, char s)
{
if (((*p) == NULL) && s != '\0')
{
*p = new treenode;
(*p)->data = s;
(*p)->left = NULL;
(*p)->right = NULL;
}
else
{
if ((*p)->data > s)
{
insert(&((*p)->left) , s);
}
else
{
insert(&((*p)->right) , s);
}
}
}
/**取begin,end中间值做根节点,两值相等相等为叶节点,大于为空**/
void createB(treenode **p, char *s, int begin, int end)
{
if (begin>end)
{
*p = NULL;
return;
} else if (begin == end)
{
*p = new treenode;
(*p)->data = s[end];
(*p)->left = (*p)->right = NULL; }
else
{
*p = new treenode;
int mid = (begin + end)/2;
(*p)->data = s[mid];
cout<<s[mid]<<endl; createB(&((*p)->left), s, begin, mid -1); createB(&((*p)->right), s, mid +1, end); }
} void depth(treenode *s, int *str,int size,int k)
{
if (s==NULL)
{
return;
}
if (s->left == NULL && s->right == NULL)
{
str[index]= k;
index++;
}
else
{
depth(s->left,str, size, k+1);
depth(s->right,str, size, k+1);
}
} treenode* create()
{
treenode *p;
char t;
cout<<"请输入:"<<endl;
t = getchar();
if (t=='#')
{
p = NULL;
}
else
{
p = new treenode;
p->data = t;
cout<<"create tree node: "<<t<<endl;
p->left = create();
p->right = create(); }
return p;
} void create(treenode **p, char *str, int i, int size)
{ if (i>size-1 || str[i] == '\0')
{
*p = NULL;
return;
} if (str[i] == '#')
{
*p=NULL;
}
else
{
*p = new treenode;
(*p)->data = str[i];
create(&((*p)->left),str,2*i+1,size);
create(&((*p)->right),str,2*i+2,size);
}
} void pOrder(treenode *p)
{
if (p==NULL)
{
return;
} cout<<p->data<<" "<<endl;
pOrder(p->left);
pOrder(p->right);
} void zOrder(treenode *p)
{
if (p==NULL)
{
return;
}
zOrder(p->left);
cout<<p->data<<" "<<endl;
zOrder(p->right);
} void hOrder(treenode *p)
{
if (p==NULL)
{
return;
}
hOrder(p->left);
cout<<p->data<<" "<<endl;
hOrder(p->right);
}
}; int main()
{
/***扩展层次序列简立树***/
//char t[9] = "ABCDE#FG";
//tree s(t);
//s.preOrder(); /***建立二叉排序树***/
//tree s;
//char t[8] = "3289654";
//s.insert(t);
// s.postOreder();
/**4.1*************************/
//cout<<"is Banlance? "<<s.isBanlance(8)<<endl; /**4.3*************************/
/*tree s;
char t[9] = "12345678";
s.createBanlance(t); s.preOrder();
*/ /**非递归层次遍历*************************/
tree s;
char t[8] = "3289654";
s.insert(t);
//s.levelOrder(); /**4.4*************************/
//vector<list<treenode *>>l=s.createList();//hawstein的方法
vector<list<treenode *>>l=s.createlist();//一个栈、两个队列
cout<<"Over"<<endl; for (int i = 0; i<l.size(); i++)
{
for (list<treenode *>::iterator j = l[i].begin(); j != l[i].end(); j++)
{
cout<< (*j)->data;
}
cout<<endl;
} return 0;
}

Cracking The Coding Interview 4.4的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. English trip V1 - B 15. Giving Personal Information 提供个人信息 Teacher:Solo Key: Do/Does

    In this lesson you will learn to answer simple questions about yourself.  本节课讲学到回答关于自己的一些简单问题 课上内容(L ...

  2. Node.js 知识(教程)

    JavaScript on the Server JavaScript was originally built for web browsers, but with Node.js we can u ...

  3. DRF之简介以及序列化操作

    1. Web应用模式. 在开发Web应用中,有两种应用模式: 前后端不分离 2.前后端分离 2. api接口 为了在团队内部形成共识.防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实 ...

  4. CRM系统之stark组件流程分析

    CRM系统主要通过自定义stark组件来实现的(参照admin系统自定义): STARK组件: 1 admin组件 1 如何使用admin 2 admin源码 3 创建自己的admin组件:stark ...

  5. MyBatis中mybatis-generator代码生成的一般过程

    MyBatis框架的使用,可以参考我的文章: https://blog.csdn.net/JayInnn/article/details/81746571(基于Mybatis实现一个查库的接口) ht ...

  6. Leetcode 1012. 十进制整数的反码

    1012. 十进制整数的反码  显示英文描述 我的提交返回竞赛   用户通过次数571 用户尝试次数602 通过次数582 提交次数1191 题目难度Easy 每个非负整数 N 都有其二进制表示.例如 ...

  7. SpringBoot利用注解@Value获取properties属性为null

    参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...

  8. Python函数基础-函数调用,定义,参数,递归

    Python内置了很多函数供调用,eg 求绝对值函数abs() >>>abs(-1) 1 >>>abs(1) 求和函数sum(),sum(iterable,star ...

  9. os模块,序列化模块,json模块,pickle模块

    一.os模块os.system("bash command") 运行shell命令,直接显示 os.popen("bash command).read() 运行shell ...

  10. 使用VAE、CNN encoder+孤立森林检测ssl加密异常流的初探——真是一个忧伤的故事!!!

    ssl payload取1024字节,然后使用VAE检测异常的ssl流. 代码如下: from sklearn.model_selection import train_test_split from ...