Cracking The Coding Interview 4.6
//原文:
//
// Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.
//
// 译文:
//
// 写程序在一棵二叉树中找到两个结点的第一个共同祖先。不允许存储额外的结点。注意: 这里不特指二叉查找树。
//
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <list>
using namespace std; struct treenode
{
char data;
treenode * left;
treenode * right;
treenode * parent;
}; class tree
{
public: tree()
{
//root = create();
root = NULL;
} ~tree()
{
/***清空二叉树***/
} /***二叉排序树:插入。***/
void insert(char *s)
{
if (s == NULL)
{
return;
} int size = strlen(s);
for (int i = 0; i<size; i++)
{
insert(&root, s[i], NULL);
}
} 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);
}
}
} treenode * findNext(treenode *p)
{ if (p->right == NULL)
{
return p->parent;
}
else
{
treenode *s = p->right;
while(s->left != NULL)
{
s = s->left;
}
return s;
}
} void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);}
treenode *root; void findLCA(treenode *p,treenode *n1,treenode *n2,treenode **lca)
{
if (p==NULL || n1 ==NULL || n2 == NULL)
{
return;
}
if (p && father(p,n1) && father(p,n2))
{
*lca = p;
findLCA(p->left,n1,n2,lca);
findLCA(p->right,n1,n2,lca); }
} private: /***判断b是否为fa的子孙***/
bool father(treenode * fa, treenode * b)
{
if (fa == NULL)
{
return false;
} else if (fa == b)
{
return true;
}
else
{
return father(fa->left,b)||father(fa->right,b);
}
} void insert(treenode **p, char s, treenode *parent)
{
if (((*p) == NULL) && s != '\0')
{
*p = new treenode;
(*p)->data = s;
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->parent = parent;
}
else
{
if ((*p)->data > s)
{
insert(&((*p)->left) , s, *p);
}
else
{
insert(&((*p)->right) , s, *p);
}
}
} 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()
{
/**非递归层次遍历*************************/
tree s;
char t[8] = "3289654";
s.insert(t);
//s.levelOrder();
//treenode *p =s.findNext((s.root)->right);
//cout<<p->data<<endl;
treenode *lca;
s.findLCA(s.root, s.root->right->right,s.root->right->left->left,&lca);
cout<<lca->data<<endl;
return 0;
}
Cracking The Coding Interview 4.6的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 雷林鹏分享:XML to HTML
XML to HTML 在 HTML 页面中显示 XML 数据 在下面的实例中,我们打开一个 XML 文件("cd_catalog.xml"),然后遍历每个 CD 元素,并显示HT ...
- 创建.ignore文件
方法一:1. 在需要创建 .gitignore 文件的文件夹, 右键选择Git Bash 进入命令行,进入项目所在目录.如:(cd /d/git/project) 2. 输入 touch .gitig ...
- 单细胞RNA-seq比对定量用什么工具好?使用哪个版本的基因组?数据来说话
这么多工具和基因组版本,选择困难症犯了,到底用哪个好呢? 2018 nature - Developmental diversification of cortical inhibitory inte ...
- 初步了解hg19注释文件的内容 | gtf
hg19有哪些染色体? chr1 chr2 chr3 chr4 chr5 chr6 chr7 chr8 chr9 chr10 chr11 chr12 chr13 chr14 chr15 chr16 c ...
- Python自学:第二章 数字 整数
>>>2 + 3 5 >>>3 - 2 1 >>>3 * 2 6 >>>3 / 2 1.5
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- SQL SERVER pivot(行转列),unpivot(列转行)
[pivot]行转列:多行变一列 假设学生成绩表Score1 Name Subject Score 小张 语文 88 小花 数学 89 小张 数学 90 Name 语文 数学 小花 null 89 小 ...
- navicat 连接 mysql 解决出现client does not support authentication protocol requested by server的问题
MySQL8换了加密插件,数据库管理客户端都来不及更新,连接方式缺乏sha2的加密方式首先第一步, UPDATE mysql.user SET plugin = 'mysql_native_passw ...
- Linux在shell中输入历史命令
在Linux的shell中,经常输入的命令有很多雷同,甚至是一样的, 如果是长命令,再次敲一遍效率真的是很低, 不过可以通过Ctl+r, 查找history中以前输入的命令,很是好用. 按Ctrl+ ...
- nginx 拒绝本地ip访问
server { listen default_server; server_name _; server_name localhost; ; }