Lowest Common Ancestor of a Binary Tree, with Parent Pointer
Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains a parent pointer which links to its parent.
int getHeight(Node* p)
{
int height = ;
while (p)
{
height++;
p = p->parent;
}
return height;
} Node* LCA(Node* p, Node *q)
{
int h1 = getHeight(p);
int h2 = getHeight(q);
if (h1 > h2)
{
swap(h1, h2);
swap(p, q);
}
for (int h = ; h < h2-h1; h++)
{
q = q->parent;
}
while (p && q)
{
if (p == q)
return p;
p = p->parent;
q = q->parent;
}
return NULL;
}
Lowest Common Ancestor of a Binary Tree, with Parent Pointer的更多相关文章
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- [LeetCode] 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 Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [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] 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 ...
随机推荐
- HDU 5727 Necklace(二分图匹配)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...
- bootstrap 响应式布局 居中问题
大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;
- mac 版本号控制工具SmartSVN7.5.4(破解版)
SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...
- 未能找到类型名称"MembershipProvider"
将用户管理程序部署到SharePoint 2013平台上,系统编译报错:未能找到类型名称"MembershipProvider",此类型已转发到程序集System.web.Appl ...
- 红豆带你从零学C#系列之:初识继承与多态
继承 现实生活当中,人类又可以根据职业分为:教师,学生,理发师,售货员 又比如飞机又有种类之分:直升飞机.客机.货机.战斗机等 在程序里面我们可能会通过创建类来描述这样的事物,比如学生类.教师类.理发 ...
- C++类的常成员函数
让一个成员函数带上常量性是什么意思呢?通常的答案是,一个常成员函数不会更改其class对象.这是一种平凡的表述,而编译器实现的手法也相当平凡. 任何非静态成员函数其实都被编译器隐式插入了一个指针类型的 ...
- Labview学习之程序Web发布
Labview学习之程序Web发布 1. LabVIEW Web服务器 在LabVIEW开发环境中,自身带了一个已连接好的Web服务器.LabVIEW Web服务器除了与其他Web服务器一样能 ...
- ASPから広がり
ASP是动态服务器页面(Active Server Page)外语缩写.[1]是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具.ASP的网页文 ...
- Django一对多,多对多操作
简要说明 Django里面的数据库操作O2O&M2M,一般归属于models管理 使用场景 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了).//两个表的 ...
- IOS 特定于设备的开发:监测Retina支持
近年来,Apple在其旗舰设备上引入了Retina显示屏.根据Apple的说法,他的像素密度非常高,足以使人眼无法区分单独的像素. UIScreen类提供了一种容易的方式,用于监查当前设备是否提供了内 ...