//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. python基础和进阶思维导图(转)

  2. sql---->sql-summary&mysql-summary

    数据库操作: 1.创建数据库,并修改默认字符编码 create database 数据库名 [charset=字符编码]; ee: create database dog charset=utf8; ...

  3. 分享WCF文件传输---WCFFileTransfer

    前几天分享了分享了WCF聊天程序--WCFChat , 本文和大家一起分享利用WCF实现文件的传输.程序运行效果:接收文件端:发送文件端:连接WCF服务,选择要传输的文件文件传输成功:我们会在保存文件 ...

  4. gem "searchkick"(4500✨) 智能搜索(使用Elastic search)(Gorails:建立一个侧边搜索栏)

    Searchkick

  5. Stark组件 (一)

    Stark组件构建 1.启动所有app下的stark.py文件,的配置实现步骤 1.创建一个Django项目crm,并创建  app1 ,app2, stark 三个app 2.在crm 项目的set ...

  6. python面向对象之 类的关系

    内容梗概: 1. 依赖关系 2. 关联关系, 组合关系, 聚合关系 3. 继承关系, self到底是什什么鬼? 4. 类中的特殊成员 1. 依赖关系def:在方法中给方法传递一个对象. 此时类与类之间 ...

  7. AS(Android Studio)不停的updating indices

    有同事问我他as进入后updating iindices个不停 就在此处一直刷一直刷,虽然对他项目没什么影响,但总归很是烦人,解决办法如下: 打开File->Invalidate Caches ...

  8. python:字典嵌套列表

    Python的字典{ }以键值对的形式保存数据,可以以键来访问字典中保存的值而不能用下标访问.字典中几乎可以包含任意的变量,字典,数列,元组.数列也一样. python的列表[ ]与字典不同,列表通过 ...

  9. 【IDEA】【5】快捷键

    前言: 1,更改快捷键:File->Settings->Keymap 2,我自定义的快捷键 shitf+alt+s getter,setter,toString方法 (修改处:Keymap ...

  10. element-ui table中排序 取消表格默认排序问题

    sortTable  设置为 custom 一定要设置在列上