二叉树 最近祖先lca + 两个结点的最小路径
http://www.acmerblog.com/distance-between-given-keys-5995.html
lca在后序遍历中找,
tralian算法还不会,懂了再补充
有了lca就好求路径了,做差而已了。
//============================================================================
// Name : TreeNodesDistance.java
// Author : GaoTong
// Date : 2014/7/26
// Copyright : www.acmerblog.com
//============================================================================ class Node{
Node left,right;
int key; public Node(int i) {
this.key = i;
}
} public class TreeNodesDistance { //返回node节点在root中的第几层,-1表示没有在root子树下找到
public static int findLevel(Node root, int node){
if(root == null) return -1;
if(root.key == node) return 0;
//先在左子树查找
int level = findLevel(root.left, node);
//左子树没有找到则到右子树查找
if(level == -1){
level = findLevel(root.right, node);
}
if(level != -1)
return level+1;
return -1;
} public static Node findLCA(Node root, int node1,int node2){
if(root == null) return null; //找到两个节点中的一个就返回
if(root.key == node1 || root.key == node2){
return root;
} //分别在左右子树查找两个节点
Node left_lca = findLCA(root.left, node1, node2);
Node right_lca = findLCA(root.right, node1, node2); if(left_lca != null && right_lca != null){
//此时说明,两个节点肯定是分别在左右子树中,当前节点比为LCA
return root;
}
return left_lca != null ? left_lca : right_lca;
} public static int distanceNodes(Node root, int node1, int node2){
Node lca = findLCA(root, node1, node2);
int dis_lca = findLevel(root, lca.key);
int dis1 = findLevel(root, node1);
int dis2 = findLevel(root, node2);
return dis1 + dis2 - 2*dis_lca;
} public static void main(String args[]){
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.right.left.right = new Node(8); System.out.println("Dist(8,7) = " + distanceNodes(root, 8,7));
System.out.println("Dist(8,3) = " + distanceNodes(root, 8,3));
System.out.println("Dist(8,3) = " + distanceNodes(root, 8,2));
}
}
二叉树 最近祖先lca + 两个结点的最小路径的更多相关文章
- 树中两个结点的最低公共祖先--java
题目:对于任意一个树,不仅仅限于二叉树,求树中两个结点的最低公共祖先结点. 解析:对于任意一棵树,显然并不局限于二叉树,也就是说树的非叶子结点可能存在多个子节点.所以,我们可以定义两个链表结构,存储这 ...
- [LeetCode] Smallest Subtree with all the Deepest Nodes 包含最深结点的最小子树
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
寻找最近公共祖先,示例如下: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ ...
- 剑指Offer - 九度1509 - 树中两个结点的最低公共祖先
剑指Offer - 九度1509 - 树中两个结点的最低公共祖先2014-02-07 01:04 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样 ...
- leetcode 236. 二叉树的最近公共祖先LCA(后序遍历,回溯)
LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百 ...
- 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ
题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...
- 【剑指Offer学习】【面试题50:树中两个结点的最低公共祖先】
题目:求树中两个结点的最低公共祖先,此树不是二叉树,而且没有指向父节点的指针. 树的结点定义 private static class TreeNode { int val; List<Tree ...
- (剑指Offer)面试题50:树中两个结点的最低公共祖先
题目: 求树中两个结点的最低公共祖先 思路: 考虑一下几种情况: 1.该树为二叉搜索树 二叉搜索树是排序树,位于左子树点的结点都比父结点小,而位于右子树的结点都比父结点大,只需要从树的根结点开始和两个 ...
- 《剑指offer》第六十八题(树中两个结点的最低公共祖先)
// 面试题68:树中两个结点的最低公共祖先 // 题目:输入两个树结点,求它们的最低公共祖先. #include <iostream> #include "Tree.h&quo ...
随机推荐
- 读Avoiding the Disk Bottleneck in the Data Domain Deduplication File System
最近在思考和实践怎样应用重复数据删除技术到云存储服务中.找了些论文来读,其中<Avoiding the Disk Bottleneck in the Data Domain Deduplicat ...
- jsckson,想说爱你不容易啊。。。406错误
最近使用spring4.0的Mvc,json请求时,客户端报错,406 Not Acceptable 解决方法一: 1.导入第三方的jackson包,jackson-mapper-asl-1.9.7. ...
- 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix
Tom and matrix Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...
- access数据库多个left join示例
代码: /// <summary> /// 分类检索 查询selectname /// </summary> public static DataTable GetSelect ...
- 在GridView列表中使用图片显示记录是否包含附件
在我的前面很多文章中,都介绍过通用附件模块的管理,本篇随笔主要介绍在一些应用模块中的列表展示中,包含附件的记录,在GridView列表界面中使用图标来快速显示是否有附件的情况. 1.通用附件模块的应用 ...
- ACCESS的参数化查询
看论坛上还许多人问及ACCESS被注入的安全问题许多人解决的方法仍然是用Replace替换特殊字符,然而这样做也并没有起到太大做用今天我就把我用ACCESS参数化查询的一些方法和经验和大家分享希望对大 ...
- inotify--内核中文件系统的通知机制
转载:http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.html 一. 引言 众所周知,Linux 桌面系统与 MAC 或 W ...
- ReactNative——生命周期
1.创建阶段 getDefaultProps:处理props的默认值 在react.createClass调用 //在创建类的时候被调用 this.props该组件的默认属性 2.实例化阶段 Reac ...
- Android性能优化之一:ViewStub
ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...
- [Xamarin.Android] Support Library Tips
[Xamarin.Android] Support Library Tips Support Library支持内容 Xamarin Support Library每个版本支持.那些组件,可以参考这份 ...