Lowest Common Ancestor of a Binary Search Tree

一、题目描写叙述

二、思路及代码

二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大。那么我们的思路就是递归比較。

假设输入的两个节点的值比当前节点小,说明是在当前根节点的左子树中;反之则在右子树中。

假设当前根节点比一个大。比一个小。那么第一个出现的这种节点就是近期的父亲节点了。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val > p.val && root.val > q.val){
return lowestCommonAncestor(root.left, p, q);
}else if(root.val < p.val && root.val < q.val){
return lowestCommonAncestor(root.right, p, q);
}else{
return root;
}
}
}

Lowest Common Ancestor of a Binary Tree

一、题目描写叙述

二、思路及代码

假设是普通的二叉树。没有了二叉搜索树的特性。就要遍历;于是我们用到DFS遍历树

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root; //找到p或者q节点,或者到最底层的叶子节点时,返回;
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; //所以假设都未找到,返回null
}
}

LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)的更多相关文章

  1. Lowest Common Ancestor of Two Nodes in a Binary Tree

    Reference: http://blog.csdn.net/v_july_v/article/details/18312089  http://leetcode.com/2011/07/lowes ...

  2. 95. Unique Binary Search Trees II (Tree; DFS)

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  4. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  5. Binary search tree or not

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. BINARY SEARCH in read table statement

    1.for standard table, it must be sorted by search key. 2.for sorted table , binary search is used au ...

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

  9. [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. Spring,Mybatis,Springmvc框架整合项目(第二部分)

    一.创建数据库表 打开Navicat Premium,点击左上角连接,选择mysql   输入你的数据库用户名和密码信息,可以先点击下测试连接,如果显示连接成功,说明能连接到数据库,然后点击确定.如果 ...

  2. TOJ 5020: Palindromic Paths

    5020: Palindromic Paths  Time Limit(Common/Java):10000MS/30000MS     Memory Limit:65536KByteTotal Su ...

  3. element-ul InputNumber 空白

    if(this.days == undefined){ this.$nextTick(function(){ this.days = 1; }); }

  4. 分析Tapjoy的模式—分发用于ios设备的企业级应用程序

    下面简单介绍下Tapjoy的模式,供大家参考: Tapjoy最初的合作模式:“按安装奖励”(pay-per-install) Tapjoy利用非常成功的奖励性下载模式影响了App Store的免费游戏 ...

  5. BZOJ 4823 [Cqoi2017]老C的方块 ——网络流

    lrd的题解:http://www.cnblogs.com/liu-runda/p/6695139.html 我还是太菜了.以后遇到这种题目应该分析分析性质的. 网络流复杂度真是$O(玄学)$ #in ...

  6. BZOJ 4503 两个串 ——FFT

    [题目分析] 定义两个字符之间的距离为 (ai-bi)^2*ai*bi 如果能够匹配,从i到i+m的位置的和一定为0 但这和暴力没有什么区别. 发现把b字符串反过来就可以卷积用FFT了. 听说KMP+ ...

  7. 北京集训TEST13——PA(Goodness)

    题目: Description 桌面上放有 n 张卡牌.对于每张卡牌,一面是绿色的,另一面是红色的.卡牌的每一面都标有一个整数.对于卡牌a和卡牌b,卡牌a对卡牌b的好感度为卡牌a绿色面的数与卡牌b红色 ...

  8. 刷题总结——火柴排队(NOIP2013)

    题目: 题目背景 NOIP2013 提高组 Day1 试题 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间 ...

  9. 算法复习——计算几何基础(zoj1081)

    题目: Statement of the Problem Several drawing applications allow us to draw polygons and almost all o ...

  10. 【2018.9.20】JOI 2017 Final T2「準急電車 / Semiexpress」

    题目描述 JOI 铁路公司是 JOI 国唯一的铁路公司. 在某条铁路沿线共有 $N$ 座车站,依次编号为 $1...N$. 目前,正在服役的车次按照运行速度可分为两类:高速电车(简称快车)与普通电车( ...