Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary search tree:  root = [3,5,1,6,2,0,8,null,null,7,4]

     _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself
according to the LCA definition.

题意:

二叉树最近公共祖先

思路:

用自底向上(bottom-up)的思路,先看看是否能在root的左子树中找到pq,再看看能否在右子树中找到,

  • 如果两边都能找到,说明当前节点就是最近公共祖先
  • 如果左边没找到,则说明pq都在右子树
  • 如果右边没找到,则说明pq都在左子树

recursion

1.  search for either of two nodes(node1, node2) whose lca starting from root

2. any of the node is found,  return that node to its parent

3. any node gets a not null node from left side and a not null node from right side, it is lca. return that node to its parent

    ___5__                                             root 5
/ \ root.left / /return 6 root.right\ \ return 4
6 _2 6 find node1 2
/ \ root.left //return null \\ return 4
7 4 7 4 find 4 node1: 6 node2: 4

code

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
// base
if (root == null || root == node1 || root == node2) {
return root;
}
// go into recursion on left side, passing same node1, node2
TreeNode left = lowestCommonAncestor(root.left, node1, node2);
TreeNode right = lowestCommonAncestor(root.right, node1, node2);
// left != null && right != null
if (left != null && right != null) {
return root; // such root is lca
}
// left!=null && right ==null
if (left != null) {
return left;
}
// right!=null && left == null
if (right!=null) {
return right;
}
// right ==null && left == null
return null;
}
}

[leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先的更多相关文章

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

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

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

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

  5. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

  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. (medium)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 OJ: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. PythonStudy——编程基础 Python Primary

    1.什么是编程语言 语言:  一个事物与另外一个事物沟通的介质 .编程语言是程序员与计算机沟通的介质. 编程: 将人类内识别的语言转化为机器能识别的指令,这种过程就叫做编程. 注:最终这些指令会被转化 ...

  2. /etc/hosts和/etc/hostname区别

    /etc/hosts主要是ip和域名的对应 /etc/hostname主要是本地主机域名(本地主机名修改过后需要重启服务器才能生效) 如果我想在另一台linux主机里面使用域名访问上面这台主机A,只需 ...

  3. zombodb 几个方便的_cat api

    zombodb 暴露所有es _cat/ api 为视图,我们可以通过视图方便的查询es 的信息,默认在zdb的schema 中 包含的视图 几个方便的view 查看索引统计信息zdb.index_s ...

  4. how to tell gcc with c99 enable

    just copy the make file here. CC = gccCFLAGS = -Wall -std=c99OUTFILE = outputfileOBJS = source.oSRCS ...

  5. 在宿主机查看docker使用cpu、内存、网络、io情况

    命令: docker stats [OPTIONS] [CONTAINER...] 显示所有: docker stats -a

  6. 记一次sql server 2005访问http接口,并解析json的过程

    记一次sql server 2005访问http接口,并解析json的过程  JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...

  7. 黄聪:xampp运行MySQL shutdown unexpectedly解决方案

    相信不少喜欢WordPress的朋友和我一样,会用xampp在本地搭建一个网站学习调试,但是在用的过程中也会遇到些麻烦,毕竟我们都是小白,今天就分享一个xampp运行MySQL数据时出现 Error: ...

  8. ThinkPHP模板的知识

    php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格 ...

  9. 围绕Buganizer的产品流程

    做技术的一定知道缺陷跟踪系统(bug系统),更不用说做测试的了,不过普遍都认为这系统是用来记录bug的,其实在google内部,这套系统是产品/项目围绕的核心.Google Buganizer扩展了类 ...

  10. 解决mysql使用GTID主从复制错误问题

    做MySQL主从的话肯定会遇到很多同步上的问题,大多数都是由于机器宕机,重启,或者是主键冲突等引起的从服务器停止工作,这里专门收集类似问题并提供整理解决方案,仅供参考. 1.主从网络中断,或主服务器重 ...