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 ...
随机推荐
- 利用fiddler模拟发送json数据的post请求
fiddler是调试利器,有许多好用的功能,这里简单的介绍一下利用fiddler模拟发送post请求的例子 先简单介绍一下失败的例子,最后给出正确的方法
- wpf中button的无边框实现
设置button的样式为:Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}";即可
- firame标签: IHTMLElement -> IHTMLFrameBase2 -> IHTMLWindow2 -> IHTMLDocument2 跨域访问
获得iframe标签的元素指针 CComPtr<IHTMLElement> spAdIframe = ... CComQIPtr<IHTMLFrameBase2> spFram ...
- OpenWRT连接OPENVPN的教程
这是相当基本没有任何web界面,只是几个命令如何运行OpenWRT的 OpenVPN的例子. OpenWRT的设置更复杂,所以这个教程仅供爱好者和经验的用户使用参考. 本教程假定您有OpenWRT的安 ...
- js 获取时间差
写这片博客 ,下面代码虽然简单,但却很实用...默默留下来... var minute = 1000 * 60;var hour = minute * 60;var day = hour * 24;v ...
- StringGrid 实例2:1、获取 StringGrid 的行数、列数; 2、给单元赋值.
实例2: 本例功能: 1.获取 StringGrid 的行数.列数; 2.给单元赋值. 运行效果图:
- shell语法快速入门(1)
#得到绝对路径 DIR=$(cd `dirname $0`;pwd) $DIR/file.txt #去掉#注释 egrep -v "(#|^$)" /etc/zabbix/zabb ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- MySQL之CAST与CONVERT 函数的用法
两者具体的语法如下:CAST(value as type); CONVERT(value, type); 可以转换的类型是有限制的.这个类型可以是以下值其中的一个: 二进制,同带binary前缀的效果 ...
- python基础整理笔记(一)
一. 编码 1. 在python2里,加载py文件会对字符进行编码,需要在文件头上的注释里注明编码类型(不加则默认是ascII). # -*- coding: utf-8 -*- print 'hel ...