//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
//
// 译文:
//
// 实现一个函数检查一棵树是否平衡。对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。 #include <iostream>
#include <string>
using namespace std; 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]);
}
} 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:
struct treenode
{
char data;
treenode * left;
treenode * right;
}; 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);
}
}
} 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(); cout<<"is Banlance? "<<s.isBanlance(8)<<endl;
cout<<"Over"<<endl; return 0;
}

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

  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. centos7【防火墙】常用规则

    1.防火墙常用规则 systemctl start iptables systemctl stop iptables systemctl restart iptablesiptables -nvL 1 ...

  2. spring cloud: Hystrix(八):turbine集群监控(dashboard)

    turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群, 因此可以通过turbine来监控集群下hystrix的metrics情况,通过eur ...

  3. tchart2

  4. Nr,GenBank, RefSeq, UniProt 数据库的异同

    Nr,GenBank, RefSeq, UniProt 数据库的异同 有的文章在做DEG分析时,会把reads比对到RefSeq的转录组上.我也没搞清楚这和直接比对到常规转录组上有什么区别. 文章:S ...

  5. “安利”一个CDN服务商网站

    一.CDN简介 CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在网络之上的内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度 ...

  6. OnSen UI结合AngularJs打造”美团"APP首页 --Hybrid Ap

    1.页面效果图: 演示链接地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_home/ 2.核心代码 home.html: <ons-page id=&q ...

  7. SSD: ReLU6

    1.在src\caffe\proto\caffe.proto中搜索message LayerParameter,在optional ReLUParameter relu_param = 123之后添加 ...

  8. 3月19 HTML静态网页的制作

    HTML :内容(Hyper Text Markup Language,超文本标记语言) <html>---开始标签 <head> 网页上的控制信息 <title> ...

  9. Thymeleaf使用bootstrap及其bootstrap相关插件(一)

    Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. 在完成信息录入界面 ...

  10. Django知识点梳理

    Django囊括.杂糅了 前端.数据库.Python知识看起来比较复杂! 其实就是由http请求周期为主体,延伸出来的知识 .  PythonWeb服务器网关接口(Python Web Server ...