Lowest Common Ancestor of Two Nodes in a Binary Tree
Reference:
http://blog.csdn.net/v_july_v/article/details/18312089
http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-ii.html
(1) Is the tree a BST or not?
BST的话,我们就能按照BST的定义思考了。当前遍历的node如果比我们要找的两个点都大,说明那两个点都在当前node的左边,所以这两个node的祖先肯定在当前node的左边,所以往左边找。反之,往右边找。如果这两个点一个大于当前node一个小于当前node,说明当前node就是LCA。 如果这两个点一个是另一个的祖先,那么这个点就是LCA。
代码如下:
(2)那如果不是BST呢?一般Binary Tree.
a. Tree结构中没有parent域。
递归找。
代码如下:
}
b. 如果有parent域。
转化为两个linkedlist的交点问题。
代码如下:
b = a - b;
a = a - b;
}
Node getLCA(Node p, Node q){
swap(h1, h2);
swap(p, q);
}
q = q.parent;
p = p.parent;
q = q.parent;
}
}
Lowest Common Ancestor of Two Nodes in a Binary Tree的更多相关文章
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode 【235. Lowest Common Ancestor of a Binary Search Tree】
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
随机推荐
- 基于PHP使用rabbitmq实现消息队列
1.从github上面获取AMQP基于php的实现扩展 2.创建生产者 send.php 3.创建消费者 receive.php 4.在cli模式下 分别执行 send.php receive. ...
- C#保存Base64格式图片
.前端页面代码 /** * 通过图片本地路径获取图片真实大小,并进行压缩 */ function getLocalRealSize(path, callback) { var img = new Im ...
- HTML5的viewport使用
viewport 语法介绍: <!-- html document --> <meta name="viewport" content=" height ...
- Dojo注意
关于插件的加载: 回调函数中的顺序,是按照插件的顺序填写的,否则就很可能会插件加载出错.
- php错误级别的设置方法
PHP在运行时, 针对严重程度不同的错误,会给以不同的提示. eg:在$a没声明时,直接相加,值为NULL,相加时当成0来算.但是,却提示NOTICE,即注意. 我们在开发中, 为了程序的规范性,把报 ...
- Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树) Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- codeforces 425E
题意:对于[l1, r1], [l2, r2]...[lm, rm]线段组成的一个集合S,我们定义f(S)为最大的不相交(没有任何公共点)线段数,现在给定n及k,n表示线段范围,即任何[li, ri] ...
- LINUX RHEL AS 4 + ORACLE10G安装详解
第一部分:LINUX RHEL AS 4 安装 运行提示: 1)按键盘的前后键可以调节光标所在的位置 2)在选项前面的括号中打上*号或者去掉*号,选中这条选项用空格键操作 3)在vi编辑文件时,键盘按 ...
- Zynga 开源其服务器性能监控分析工具 zPerfmon
流年不利的社交游戏服务商Zynga近日在官方博客宣布开源zPerfmon——该工具负责监控Zynga数以千计的社交游戏服务器.zPerfmon的源代码目前已经上传至Github. 包括Facebook ...
- Makeflow 4.0 发布,工作流引擎
Makeflow 4.0 发布了,主要改进包括: 1. 支持分层次的 workers,带 master-foremen-workers 范式. 2. 一个 worker 可同时处理超过 1 个的任务3 ...