[抄题]:

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

“The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

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

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

[思维问题]:

不知道子节点怎么用dc。直接对给出的p,q节点进行操作即可。

[一句话思路]:

左右分开 谁不空返回谁

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

(left != null && right != null) 时,返回的是root节点的结果,不需要再做递归运算了。是一个“合”的过程。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

只有dc算法,没有数据结构

[其他解法]:

自己写traverse函数:不好,会形成全局变量

[Follow Up]:

有parent指针的:用对齐的方法做

[LC给出的题目变变变]:

Lowest Common Ancestor of a Binary Search Tree 一模一样的,约束条件没用,直接套。

public class Solution {
/*
* @param root: The root of the binary search tree.
* @param A: A TreeNode in a Binary.
* @param B: A TreeNode in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
if (root == null || A == root || B == root) {//
return root;
}
//divide
TreeNode left = lowestCommonAncestor(root.left, A, B);
TreeNode right = lowestCommonAncestor(root.right, A, B); //conquer
if (left != null && right != null) {
return root;//
}
else if (left != null) {
return left;
}
else if (right != null) {
return right;
}
else {
return null;
}
}
}

最近公共祖先 · Lowest Common Ancestor的更多相关文章

  1. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | 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 ...

  2. [Swift]LeetCode236. 二叉树的最近公共祖先 | 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. 最近公共祖先 Lowest Common Ancestors

    基于深度的LCA算法:  对于两个结点u.v,它们的深度分别为depth(u).depth(v),对于其公共祖先w,深度为depth(w),u需要向上回溯depth(u)-depth(w)步,v需要d ...

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

  5. 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)

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

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

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

  8. LeetCode OJ: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之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)

    Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...

随机推荐

  1. MySQL concat用法举例

    concat配合information_schema的应用 1    concat的一般用法主要是用于拼接 示例: 执行语句 SELECT CONCAT('M','y','S','Q','L') 可以 ...

  2. windows 网管常用命令

    Windows网络命令行程序 这部分包括: 使用 ipconfig /all 查看配置 使用 ipconfig /renew 刷新配置 使用 ipconfig 管理 DNS 和 DHCP 类别 ID ...

  3. zabbix企业应用:通过SNMP和iDRAC监控DELL服务器硬件

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://qicheng0211.blog.51cto.com/3958621/174998 ...

  4. linux常用命令解析

    linux下一些注意事项 1. linux下严格区分大小写 ls 简述:列出文件或目录列表. -> ls 默认列出当前目录下的所有文件. -> ls -l(long)以长格式查看文件. - ...

  5. CSS3 的calc()方法的使用

    calc()简单介绍 calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我, ...

  6. WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException

    Hadoop 2.7.4 The reason is this: originally, DataStreamer::closeResponder always prints a warning ab ...

  7. [Python] 拉格朗日插值

    #-*— coding:utf-8 -*- #Program 0.3 Lagrange Interpolation import matplotlib.pyplot as plt import num ...

  8. Bogart BogartPublic.vb

    Imports System.Data.SqlClient Imports System.Data #Region "IBogartToolbar,請勿隨便更改" Interfac ...

  9. selenium ie 模拟request pahonjs

  10. 0_Simple__simpleSeparateCompilation

    ▶ 简单的将纯 C/C++ 函数放到另一个文件中,利用头文件引用到主体 .cu 中来,编译时共同编译. ▶ 源代码,把 C++ 的部分去掉了 // simpleDeviceLibrary.cuh #i ...