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 ...
随机推荐
- Android4: Write Storage权限问题
原文:Android4: Write Storage权限问题 2.3中声明 <uses-permission android:name="android.permission.WRIT ...
- git多人协作
多人协作 当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- 剑指offer26 复杂链表的复制
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...
- adb server is out of date.
1:今天调试android的时候发现一个诡异的问题 C:\Users\xxxx>adb start-server adb server is out of date. killing... A ...
- HTTP错误500.22 检测到在集成的托管管道模式下不适用的ASP.NET设置
这里主要把集成模式改成经典模式 解决方案一: 解决方案二: 修改配置文件web.config 将 <configuration> <system.web> <compil ...
- NET项目反编译+VS解决方案整理流程
net项目反编译 工具:De4Dot + IL SPY和Reflector结合使用 项目:vs10+创建解决方案,每个类库尽量按照dll名来命名,方便整合,新建web项目先把aspx等文件拷贝进去,注 ...
- http 请求安全
在info.plist中加入 <key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbit ...
- 如何实现调用console.log(‘good’.repeat(3))时输出goodgoodgood?
String.prototype.repeat=function(num){ return (new Array(num+1)).join(this) } console.log('good'.rep ...
- 获取select赋值
<select class="sel-ul-add" id="xuanzhe"> <option>A</option> &l ...