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的交点问题。

代码如下:

              a = a + b;
         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的更多相关文章

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

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

  3. 数据结构与算法(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 ...

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

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

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

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

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

  9. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

随机推荐

  1. [python] 线程简介

    参考:http://www.cnblogs.com/aylin/p/5601969.html 我是搬运工,特别感谢张岩林老师! python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件 ...

  2. C#字段中加入list<类字段> 的两种写法

    类1 public class NumCon { public string zsNum { get; set; } } 类2 public class RepeatMess //重复数据响应 { p ...

  3. 内存管理运算符new delete与内存管理函数malloc free的区别——已经他们对对象创建的过程。

    (1)内存管理函数与内存管理运算符的区别 内存管理函数有内存分配函数,malloc calloc realloc 以及内存释放函数free. 内存管理运算符有new 和delete. 两种内存管理方式 ...

  4. LeetCode OJ-- String to Integer (atoi) **

    https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...

  5. 解决TextView最后一行显示不全

    public class MultilineTextView extends TextView { private boolean calculatedLines = false; public Mu ...

  6. 【洛谷P2296】寻找道路

    反正图两边bfs #include<iostream> #include<cstdio> #include<queue> using namespace std; ...

  7. DBHelp数据处理类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. LDO-BD00C0AWFP

    BD00C0AWxx  1.该产品是ROHM公司的一款高输入电压可达26.5V,输出电压可达15V,输出电流1A的LDO.具有较低的ESR Capacitor. 2.输入电压变化4-26.5V,输出3 ...

  9. 从RTSP协议SDP数据中获得二进制的SPS、PPS

    在RTSP协议的交互过程中,第二步客户端发送DESCRIBE请求之后,服务端会返回SDP内容,该SDP内容中有关于媒体和会话的描述,本篇文章主要给出如何从SDP字符串中得到H264视频信息中的sps. ...

  10. ftp客户端命令使用简记

    OS:windows8.1评估版 程序和功能 tftp客户端勾选上 Win+R:运行,键入cmd,键入ftp -help 如下图: 使用ftp客户端可以做的事:将文件传送到运行FTP服务器服务(经常称 ...