给定一个二叉树和所要查找的两个节点,找到两个节点的最近公共父亲节点(LCA)。比如,节点5和1的LCA是3,节点5和4的LCA是5。

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);//如果p在左子树,则会返回p,否则返回空
TreeNode right = lowestCommonAncestor(root.right, p, q);//如果q在右子树,则会返回q,否则返回空
if(left != null && right != null){
return root;//如果左右都不为空,则root肯定是祖先节点
}
return left == null ? right : left;
}
}

转载于:https://www.cnblogs.com/MarkLeeBYR/p/10536085.html

236.Lowest Common Ancestor of a BinaryTree相关推荐

  1. 236 Lowest Common Ancestor of a Binary Tree

    题目: 236 Lowest Common Ancestor of a Binary Tree 这道题和 235 基本一样 class Solution:# @param {TreeNode} roo ...

  2. 236. Lowest Common Ancestor of a Binary Tree

    原题链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ 代码实现如下: impo ...

  3. leetcode 236. Lowest Common Ancestor of a Binary Tree | 236. 二叉树的最近公共祖先(Java)

    题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题解 思路来源:左程云<程序员代码面试指南&g ...

  4. Lowest Common Ancestor of a Binary Search Tree a Binary Tree

    235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...

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

  6. 235 Lowest Common Ancestor of a Binary Search Tree

    题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...

  7. C语言二叉树的lowest common ancestor最低公共祖先(附完整源码)

    C语言二叉树的lowest common ancestor最低公共祖先 C语言二叉树的lowest common ancestor最低公共祖先完整源码(定义,实现,main函数测试) C语言二叉树的l ...

  8. C++lowest common ancestor最近公共祖先算法(附完整源码)

    C++lowest common ancestor最近公共祖先算法 C++lowest common ancestor最近公共祖先算法完整源码(定义,实现,main函数测试) C++lowest co ...

  9. Leet Code OJ 235. Lowest Common Ancestor of a Binary Search Tree [Difficulty: Easy]

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

  10. leetcode——Lowest Common Ancestor of a Binary Tree

    题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...

最新文章

  1. 题解 P4692 【[Ynoi2016]谁的梦】
  2. Java中父类方法重写有哪些需要注意的?
  3. 金秋十月正当时,未知君招人啦!
  4. mysql报错:Column 'id' in field list is ambiguous,以及tp的三表联合查询语句,打印sql等
  5. UICollectionView框架总结
  6. CAN总线数据帧/标准帧/扩展帧/远程帧/错误帧的组成格式对比
  7. android 商品筛选_商品关联分析
  8. PyTorch并行与分布式(二)分布式通信包torch.distributed
  9. iOS开发之cocoapods报错MacOSSierra10.13-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framewor
  10. MySql数据库帮助类:DbHelperMySQL
  11. android 高仿网易新闻,Android实战:仿网易新闻app--简介
  12. CDC的DrawText
  13. 未来-区块链-Aliyun:阿里云IoT - 所知不止于感知
  14. [JavaWeb实训Day3]__button_框架布局点击事件
  15. Android Google Map集成以及部分功能的实现
  16. 为什么经常把VARCHAR长度设置成255,而不是更大
  17. CSS3实现的线条波浪动画效果
  18. dedecms编辑器 php,dede编辑器怎么换成kindEditor编辑器
  19. 大众服务器维护是,上海大众ODIS在线刷新操作说明
  20. PyTorch 深度学习实践 (4)构建线性回归模型

热门文章

  1. jcr一区是什么意思_SCI分区中JCR分区和中科院分区的三点区别
  2. 游戏开发之动态创建对象及构造函数、析构函数的执行(C++基础)
  3. MySQL导入与导出备份详解
  4. SNMP原理及常用配置命令
  5. DHCP技术原理详解
  6. IPv6中NDP协议简介
  7. centos7.4批量自动化安装(pxe、cobbler、制作自动化iso镜像);pxe安装;cobbler安装;
  8. laravel5.5 Auth门脸类认证
  9. 并发控制技术手段之多版本(三)
  10. Cisco ppp链路单双认证