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 tree:  root = [3,5,1,6,2,0,8,null,null,7,4]

/**
* 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 p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
}
return left != null ? left : right;
}
}

[LC] 236. Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  2. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  3. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  4. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

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

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

  7. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

随机推荐

  1. c#连接sql server数据库字符串

    第一种方式 Data Source=数据库地址;Initial Catalog=数据库名称;User Id=数据库登录名;Password=数据库密码;[Integrated Security=SSP ...

  2. Ubuntu---VIM 常用命令

    今天学习 VIM 的一些常用命令,向传说中的“最后一个编辑器”进攻,哈哈 插入命令: # insert i : 当前光标之前插入 I : 在此行的行首插入 o : 在下一行新起一行插入 O : 在上一 ...

  3. 将iso mount 到nfs 目录问题

    最近有个需求,需要在多台系统安装程序,安装文件是iso 格式的,最普通的办法就是拷贝到其它系统,然后mount loop 到本地目录. 但是比较麻烦,而且当前已经有一个nfs 服务端了,于是想出一个办 ...

  4. sybase连接失败 JZ006: Caught IOException: java.net.ConnectException处理方式

    windows系统下的处理办法: 1.查找端口为5000的进程的pid: 在cmd窗口中输入 netstat -ano 我这里是2324. 打开任务管理器,找到pid是2324的进程,结束进程. 打开 ...

  5. 15 docker 网络 docker 容器之间的关系 docker link

    1.案例:使用 link 关联后台与数据库 创建 test1 容器 docker run -d --name test1 busybox /bin/sh -c "while true; do ...

  6. 函数(Python)

    函数是什么? 计算机的函数,是一个固定的一个程序段,或称其为一个子程序,它在可以实现固定运算功能的同时,还带有一个入口和一个出口,所谓的入口,就是函数所带的各个参数,我们可以通过这个入口,把函数的参数 ...

  7. 初识数据库MySQL

    一.认识数据库  1:什么是数据(Data) 描述事物的符号记录被称为数据,这个符号可以是数字,文字,图片,声音,语言等 2:什么是数据库(DataBase,简称DB) 数据库是存放数据的仓库,库一般 ...

  8. Oracle数据库添加删除主外键

    (一)添加主键 1.表创建的同时,添加主键约束 语法: create table "表名" ( "列名1" 数据类型及长度 constraint "主 ...

  9. 对象数组和for循环遍历输出学生的信息

    public class Student { private int no; private String name; private int age; public Student(int no, ...

  10. LeetCode No.109,110,111

    No.109 SortedListToBST 有序链表转换二叉搜索树 题目 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的 ...