[抄题]:

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. 什么是DSCP,如何使用DSCP标记搭配ROS策略

    一.什么是DSCP DSCP:差分服务代码点(Differentiated Services Code Point),IETF于1998年12月发布了Diff-Serv(Differentiated ...

  2. vcenter修改用户密码的方法

    https://192.168.x.x:9443登录,必须用administrator@vsphere.local登录,不能用root用户登录. 主页-系统设置- Single Sing-On-用户和 ...

  3. js如何判断小数点后有几位

    <script> var n=3.143423423;alert(n.toString().split(".")[1].length); </script> ...

  4. linux nfs怪现象——软连接、文件属主的变更

    怪现象:proxmox:/etc-asterisk# ls sip.confsip.confproxmox:/etc-asterisk# more sip.confsip.conf: No such ...

  5. spyder快捷键

    ctrl+1:注释/反注释 ctrl+4/5:注释/反注释 tab/ shift+tab:缩进/反缩进 F5:全运行 F9:单行运行 F11:全屏 ctrl+I:显示帮助

  6. Linux常用命令收藏

    常见操作: 快速清空文件内容的几种方法:#: > filename # > filename # echo "" > filename # echo > f ...

  7. (转)手机的AP和BP是什么?

    AP:Application Processor,即应用芯片 BP:Baseband Processor,即基带芯片 搞什么嘛,双核就双核呗,怎么又搞出个AP和BP啊 原来,FCC(美国联邦通信委员会 ...

  8. Asynchronous programming with Tornado

    Asynchronous programming can be tricky for beginners, therefore I think it’s useful to iron some bas ...

  9. 配置IIS,以在局域网内访问发布的web站点

    在windows 7或win8 中 配置IIS, 以在局域网内访问自己发布的web 网站或应用程序.主要配置步骤如下: 1. 打开 win7 或 win8 控制面板,选择: 打开或关闭windws 功 ...

  10. vconsole h5应用ajax请求抓包

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...