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. kafka-producer配置

    kafka-producer版本对比 Kafka的producer的API根据版本的不同分为kafka0.8.1.X之前的 kafka.javaapi.producer.Producer.以及之后版本 ...

  2. python制作模块

    自己写的函数,为了下一次方便用,做成模块 主要有这几个步骤: 1:准备发布 2:构建发布 3:导入模块并使用 1:准备发布 首先,我自己写的一个打印出列表(含嵌套列表),打印出列表中的每个数据项,文件 ...

  3. 使用新标签兼容低版本IE

    HTML语义化 意义:根据内容的结构化(语义化),选择合适的标签,便于开发者阅读和写出更优雅的代码,同时让流浪器的爬虫和机器更好的解析. 尽可能少的使用无语义的标签 div 和 span 在语义不明显 ...

  4. 剑指offer 3. 链表 从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 解题思路:利用栈先进后出的原理,依次把ArrayList的值入栈,再出栈即可逆序 import java.util.Arra ...

  5. delphi字符串分割

    function GetLeft(sText, sepStr: string): string; var p: Integer; begin p := Pos(sepStr, sText); then ...

  6. oidc User.Identity.Name 为空解决方法

    public override Task TicketReceived(TicketReceivedContext context) { var result = base.TicketReceive ...

  7. NPOI 操作Word

    /// <summary> /// 替换word中指定内容 /// </summary> /// <param name="wordPath"> ...

  8. 廖雪峰Java7处理日期和时间-1概念-1日期和时间

    1.日期 日期是指某一天,如2016-11-20,2018-1-1 2.时间有2种: 不带日期的时间:14:23:54 带日期的时间:2017-1-1 20:21:23,唯一确定某个时刻 3.时区 时 ...

  9. IDEA忽略某些文件

    Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容   下图为我自己添加过滤的内容,例如:*.im ...

  10. 导出Excel(Ext 前台部分)

    开发思路: - 序列化当前GridPanel 数据, 表头结构(用于对应关系), 通过控制器Aspose写到Excel中, 然后返回临时文件地址, 弹出窗口下载. function btnExport ...