Lintcode: Lowest Common Ancestor
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.
The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.
Example
/ \
/ \
For and , the LCA is .
For and , the LCA is .
For and , the LCA is .
更复杂的参考:http://www.cnblogs.com/EdwardLiu/p/4265448.html
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param A and B: two nodes in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
// write your code here
if (root == null) return null;
if (root==A || root==B) return root;
TreeNode lch = lowestCommonAncestor(root.left, A, B);
TreeNode rch = lowestCommonAncestor(root.right, A, B);
if (lch!=null && rch!=null) return root;
return lch==null? rch : lch;
}
}
Lintcode: Lowest Common Ancestor的更多相关文章
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- 【Lintcode】088.Lowest Common Ancestor
题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...
- [OJ] Lowest Common Ancestor
LintCode 88. Lowest Common Ancestor (Medium) LeetCode 236. Lowest Common Ancestor of a Binary Tree ( ...
- [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. According ...
- [LeetCode] 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 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- [LeetCode]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 ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- 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 ...
随机推荐
- Sencha Touch 实战开发培训 视频教程 第二期 第四节
2014.4.14 晚上8:10分开课. 本节课耗时没有超出一个小时,主要讲解了list的一些扩展用法. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: List的高级应用 ...
- ZOJ 3435 Ideal Puzzle Bobble
ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds Memory Limit: 65536 KB Have yo ...
- 实战BRTSvc一款我见过的最嚣张的挖矿软件
第一步:发现告警 Suricata发现特征字符串jsonrpc,这个是匹配挖矿木马的一个重要特征.于是开始分析告警信息: 告警中可以提取出的有效信息如下: 目标IP:149.28.199.108 目标 ...
- mac设置文件权限问题
在使用mac时,经常我们遇到相关文件不能使用的情况,其实大多数情况都是,文件权限问题. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁止对其做任何的更改操 ...
- Redis的启动过程
本文主要介绍Redis启动加载过程,总体上可以分为如下几步: 1. 初始化全局服务器配置 2. 加载配置文件(如果指定了配置文件,否则使用默认配置) 3. 初始化服务器 4. 加载数据库 5. 网络监 ...
- java成员变量和局部变量的初始化和内存中的运行机制
成员变量: 当系统加载类或创建类的实例时,系统会自动为成员变量分配内存空间,并在分配内存空间后,自动为成员变量指定初始值. eyeNum是类属性.name是实例属性 所有person实例访问eyeNu ...
- HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...
- abi-api, arm target triplet https://en.wikipedia.org/wiki/ARM_architecture
GNU软件用target triplet来描述工作平台,target triplet是一种规范化的名称,形如cpu-vendor-os(where os can be ‘system’ or ‘ker ...
- kerberos (https://en.wikipedia.org/wiki/Kerberos_(protocol))
Protocol[edit] Description[edit] The client authenticates itself to the Authentication Server (AS) w ...
- html中label及加上属性for之后的用法
定义和用法 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 label 元 ...