//原文:
//
// 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的更多相关文章

  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. oracle查看表中否存在某字段,数据库是否存在某张表

      数据库是否存在某字段 SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '表名' AND COLUMN_NAME = '字段名'; ...

  2. Python入门学习指南--内附学习框架

    https://blog.csdn.net/weixin_44558127/article/details/86527360

  3. p1530 Fractions to Decimals

    将余数记录下来,如果余数相同,那么商的下一位也相同. #include <iostream> #include <cstdio> #include <cmath> ...

  4. Java -------- 首字母相关排序总结

    Java 字符串数组首字母排序 字符串数组按首字母排序:(区分大小写) String[] strings = new String[]{"ba","aa",&q ...

  5. 集合 (set)

    set 是一个无序不重复的元素集,集合跟字典是无序的,不支持索引 创建集合: 第一种方式:通过{ }创建 >>> num={1,2,3,4,3,3,1} >>> n ...

  6. vue 点击一个div,使input获得焦点

    <div class="inputMessage" @click="inputMessage">输入留言</div> <input ...

  7. 子类父类(虚函数下的 引用指针 对象)->看来没有子类指针这回事

    #include<iostream> using namespace std; class Father { public: Father() { cout << " ...

  8. poj-1026-置换

    Cipher Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Public Key ...

  9. HTML 5 <span> 标签

    标签定义及使用说明 <span> 用于对文档中的行内元素进行组合. <span> 标签没有固定的格式表现.当对它应用样式时,它才会产生视觉上的变化.如果不对 <span& ...

  10. bootstrap居中

    1.页面 <div class="container"> <div class="row clearfix"> <div clas ...