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的更多相关文章

  1. 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 利用二 ...

  2. 【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 ...

  3. 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 ...

  4. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  5. 【刷题-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 ...

  6. [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 ...

  7. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Ubuntu启动、停止、重新启动MySQL,查看MySQL错误日志、中文编码错误

    1)启动: sudo /etc/init.d/mysql start 2)停止: sudo /etc/init.d/mysql stop 3)重新启动: sudo /etc/init.d/mysql ...

  2. md笔记——正则学习

    正则表达式 在线调试正则1 在线调试正则2 规则记录 \d 匹配一个数字字符.等价于[0-9] \D 匹配一个非数字字符.等价于[^0-9]. . 通配符,可以匹配任意字符. ? 表示量词" ...

  3. English - when用法

    一.作为副词,它有以下的用法:  1. 作为疑问副词,引导特殊疑问句,意为“什么时候:何时”.如: ( 1 ) When will they come back?( 2 ) What time wil ...

  4. AdventureWorks2008 数据库安装

    我使用的操作系统是 win 8.1,由于对早前安装的sql server 2008的兼容性不太好,要安装对应的service pack来解决一下这个问题. 如何使用 SQL Server 在 Wind ...

  5. .net string format

    转自:http://www.cnblogs.com/jobs2/p/3948049.html 转自:http://jingyan.baidu.com/article/48206aeaf8c52f216 ...

  6. VS2013无法链接到TFS (转)

    VS2013无法链接到TFS(Visual studio online),错误TF31001,TF31002   TF31002: Unable to connect to VisualStudio ...

  7. shell检测interface是否已分配ip,qt调用shell脚本

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  8. 使用#define定义字面值和伪函数

    #define是C语言提供的宏命令,其主要目的是:在编程时,为程序员提供一定方便,并能在一定程度上提高程序的执行效率.#define将一个标示符定义为一个字符串,该标示符被称为宏,被定义的字符串称为字 ...

  9. python学习之 dictionary 、list、tuple操作

    python 内置类型数据 有dictionary(字典).list(列表)和tuple(元组) 一.Dictionary Dictionary 是 Python 的内置数据类型之一,它定义了键和值之 ...

  10. BZOJ 2245: [SDOI2011]工作安排( 费用流 )

    费用流模板题..限制一下不同愤怒值的工作数就可以了. ------------------------------------------------------------------------- ...