import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack; //树中两个节点的最低公共祖先
//第一种情况:只是一颗二叉树,而且还是排序二叉树。思路:从根节点开始找起,如果这两个数一个大于
//根节点,一个小于根节点,那么最低公共子节点就是根节点。
//第二种情况:只是一颗普通的树,但是有指向父节点的指针。
//那么就变成 了两个链表求第一个公共节点的情况。
//第三种情况:只是一个普通的树,而且没有指针指向根节点。那么就只能用栈来存放遍历了的节点。找到相同的节点为止。
public class TreeLastCommonParent {
public class BiTreeNode{
int m_nValue;
BiTreeNode m_pLeft;
BiTreeNode m_pRight;
BiTreeNode m_pParent;
}
public BiTreeNode createBiTree(int[] pre,int start,int[] ord,int end,int length){
if(pre.length!=ord.length||pre==null||ord==null||length<=0)
return null;
int value=pre[start];
BiTreeNode root=new BiTreeNode();
root.m_nValue=value;
root.m_pRight=root.m_pLeft=null;
if(length==1){
if(pre[start]==ord[end])
return root;
else
throw new RuntimeException("inVaild put");
}
//遍历中序遍历的序列找到根节点
int i=0;
while(i<length){
if(ord[end-i]==value)
break;
i++;
}
int right=i;
int left=length-i-1;
if(left>0)
root.m_pLeft=createBiTree(pre,start+1,ord,end-i-1,length-i-1);
if(right>0)
root.m_pRight=createBiTree(pre,start+length-i,ord,end,i);
return root;
}
//该树是二叉树,而且还是排序二叉树。
public BiTreeNode commonParent1(BiTreeNode pHead,BiTreeNode p1,BiTreeNode p2){
if(pHead==null)
return null;
if(p1.m_nValue<pHead.m_nValue&&p2.m_nValue>pHead.m_nValue
||p2.m_nValue<pHead.m_nValue&&p1.m_nValue>pHead.m_nValue){
return pHead; }
else if(p1.m_nValue<pHead.m_nValue&&p2.m_nValue<pHead.m_nValue){
return commonParent1(pHead.m_pLeft,p1,p2); }
else
return commonParent1(pHead.m_pRight,p1,p2);
}
//该树有指向父节点的指针
public BiTreeNode commonParent2(BiTreeNode pHead,BiTreeNode p1,BiTreeNode p2){
if(pHead==null)
return null;
int len1=0;
int len2=0;
while(p1!=pHead){
len1++;
p1=p1.m_pParent;
}
while(p2!=pHead){
len2++;
p2=p2.m_pParent;
}
int dif=len1-len2;
BiTreeNode longest=p1;
BiTreeNode shortest=p2;
if(dif<0){
dif=len2-len1;
longest=p2;
shortest=p1;
}
for(int i=0;i<dif;i++)
longest=longest.m_pParent;
while(longest!=shortest){
longest=longest.m_pParent;
shortest=shortest.m_pParent;
}
return longest;
}
//该树没有指向父节点的指针
public BiTreeNode commonParent3(BiTreeNode pHead,BiTreeNode p1,BiTreeNode p2){ if(pHead==null)
return null;
Queue<BiTreeNode> path1=new LinkedList<BiTreeNode>();
Queue<BiTreeNode> path2=new LinkedList<BiTreeNode>();
Queue<BiTreeNode> longPath=path1;
Queue<BiTreeNode> shortPath=path2;
BiTreeNode head=pHead;
getNodePath(pHead,p1,path1);
pHead=head;
getNodePath(pHead,p2,path2);
int dif=path1.size()-path2.size(); if(dif<0){
dif=path2.size()-path1.size();
longPath=path2;
shortPath=path1;
}
for(int i=0;i<dif;i++){
longPath.remove();
}
while(longPath.peek()!=shortPath.peek())
{
longPath.remove();
shortPath.remove();
} return longPath.peek(); } public boolean getNodePath(BiTreeNode pHead,BiTreeNode p,Queue<BiTreeNode> path1){
if(pHead==null)
return false; if(pHead==p||getNodePath(pHead.m_pLeft,p,path1)||
getNodePath(pHead.m_pRight,p,path1)){
path1.add(pHead);
return true;
}
return false; } public static void main(String[] args){
int[] pre={8,4,3,7,9};
int[] ord={3,4,7,8,9};
TreeLastCommonParent treeLastCommon=new TreeLastCommonParent();
BiTreeNode pHead=treeLastCommon.createBiTree(pre,0,ord,pre.length-1,pre.length);
BiTreeNode p1=pHead.m_pLeft.m_pRight;
BiTreeNode p2=pHead.m_pLeft.m_pLeft;
BiTreeNode pCommon=treeLastCommon.commonParent1(pHead, p1, p2);
BiTreeNode pCommon3= treeLastCommon.commonParent3(pHead, p1, p2);
System.out.println(pCommon.m_nValue+" ");
System.out.println(pCommon3.m_nValue+" ");
}
}

剑指offer-第七章面试案例2(树中两个节点的公共祖先节点)的更多相关文章

  1. 剑指offer第七章&第八章

    剑指offer第七章&第八章 1.把字符串转换成整数 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串 ...

  2. 剑指Offer(第二版)面试案例:树中两个节点的最低公共祖先节点

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74612786冷血之心的博客) 剑指Offer(第二版)面试案例:树 ...

  3. 剑指offer第六章

    剑指offer第六章 1.数字在排序数组中出现的次数 统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在数组中出现了4次,所以输出4 分析:思路1 ...

  4. 剑指offer第五章

    剑指offer第五章 1.数组中出现次数超过一半的数 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组 ...

  5. 剑指offer第四章

    剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...

  6. 剑指offer第三章

    剑指offer第三章 1.数值的整数次方 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class Solution { public ...

  7. LeetCode题解汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...

  8. 剑指Offer - 九度1352 - 和为S的两个数字

    剑指Offer - 九度1352 - 和为S的两个数字2014-02-05 18:15 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于 ...

  9. 《剑指Offer》第二章(一)题3-8

    为春招实习做准备,记录一下<剑指Offer>里面的面试题 第二章 面试题3:数组之中的重复数字. 这个题吧,虽然不难,但是不知道为什么就是看了很久,可能很久没有做算法题了.最后面一句话说的 ...

  10. LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...

随机推荐

  1. Mysql:实现分组查询拼接未分组同一字段字符group_concat()

    Mysql:实现分组查询拼接未分组同一字段字符group_concat() MySQL中,如果想实现将分组之后的多个数据合并到一列,可以使用group_concat函数,如下图所示: 在oralce中 ...

  2. iOS7中彻底隐藏status bar

    用Xcode5开发新游戏,发现在iOS7中按照以前的方法隐藏status bar失效了. 想要彻底隐藏status bar,需要在info.plist中添加新行“View controller-bas ...

  3. windows 下android react native详细安装配置过程

    写在前面: 在网上搜了很多安装配置文档,感觉没有一个真的跟我安装的过程一模一样的,东拼拼西凑凑,总算是装好了,我不会告诉你,断断续续,我花了两天时间...一到黑屏报错就傻眼,幸好在react群里遇到了 ...

  4. Can't bind multiple parameters ('header' and 'parameters') to the request's content.

    2019-01-23 15:46:29.012+08:00 ERROR [6]: System.InvalidOperationException: Can't bind multiple param ...

  5. Oracle imp 导入数据出现 ORA-12560

    错误如下: D:\software\xfwebdb2015-05-11\autobackup>imp Import: Release 10.2.0.1.0 - Production on 星期三 ...

  6. Python中求阶乘(factorial)

    1. math.factorial(x) import math value = math.factorial(x) 2. reduce函数 def factorial(n): return redu ...

  7. maven中pom.xml标签介绍

    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...

  8. 【JAVA学习】java虚拟机内存配置,-Xss256m -Xms512m -Xmx800m -XX:MaxPermSize=512m

    转自:https://blog.csdn.net/a503921892/article/details/39048737 配置tomcat服务器内存大小中的Xms.Xmx.PermSize.MaxPe ...

  9. 使用maven时报错Dynamic Web Module 3.1 requires Java 1.7 or newe

    解决方法:        1. 在eclipse 构建 web中关于java版本有三处需要修改统一.            (1)在 Java Build Path的libraries中修改      ...

  10. js进阶---12-12、jquery事件委托怎么使用

    js进阶---12-12.jquery事件委托怎么使用 一.总结 一句话总结:通过on方法(事件委托),给要绑定事件的元素的祖先绑定事件,从而达到效果. 1.事件委托是什么? 通过事件冒泡,让子元素绑 ...