Lowest Common Ancestor of a Binary Tree
来源:互联网 发布:网络配线架品牌 编辑:程序博客网 时间:2024/06/06 14:17
思路为:
找到p,q,的节点,利用递归从而找到p,q的共公父节点,返回的父节点即为所求的节点
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root== null) return null;if(root==p) return root;else if(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;else{return left==null?right:left;} }
0 0
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree(***)
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- toupper()函数
- 响应状态代码
- 【Java】happyNumber
- 分布式事务方式
- Redis源码解析:23sentinel(四)故障转移流程
- Lowest Common Ancestor of a Binary Tree
- 以一个简单的数据库表为例来展示创建全局/局部数据源和连接池的配置与测试
- cocos2dx学习源代码之接口iOS事件分发(2)
- Swift基础(六):属性、方法、下标脚本
- 六种处理异常的陋习
- 【leetcode】242. Valid Anagram
- python导入package
- EDK14.7 ip核向导 驱动文件bug修复
- 百度地图定位源码学习