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. ubuntu 执行apt-get update报错Failed to fetch

    在ubuntu下执行sudo apt-get update时,经常会遇到报错: Err http://security.ubuntu.com precise-security InRelease Er ...

  2. Yii2.0 添加分类category model类

    <?php namespace app\models; use yii\db\ActiveRecord; use Yii; use yii\helpers\ArrayHelper; class ...

  3. 07-复习数组和简单api

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. 写给新员工的十点SQL开发建议

    1.建立自己的知识体系 摘抄一句话你所拥有的知识并不取决于你记得多少,而在于它们能否在恰当的时候被回忆起来: 做笔记: 把笔记放在可以随时被找到的地方.个人的笔记可以放在印象笔记之类工具上,单位上的笔 ...

  5. Redis常见配置redis.conf

    redis的配置文件.相信学过SSH或SSM的读者都知道,配置文件的使用在当下开发已十分普遍,希望大家要熟悉习惯这 种开发方式,废话不多说,来开始我们今天的内容吧. 首先得找到 redis 的配置文件 ...

  6. 关于java读取文件IO流学习总结(二)

    今天网上随意逛了逛,某处看到关于文件的操作,描述的不错,加深了对于文件操作的理解,在此分享给大家.希望能够有所收获. java读取txt文件内容: 可以作如下理解: 1. 首先获得一个文件句柄.Fil ...

  7. SHOWMODALDIALOG表单提交时禁止打开新窗口

    前提条件:showmodaldialog中有表单form.当action="#"的时候,提交表单,不会打开新窗口,但这种#自提有时不能用,#是本页面完整的带参数的url,如果表单中 ...

  8. jsp 详解request对象

    request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1  object ...

  9. Spring-IOC源码解读3-依赖注入

    当容器已经载入了BeanDefinition的信息完成了初始化,我们继续分析依赖注入的原理,需要注意的是依赖注入是用户第一次向IOC容器获取Bean的时候发生的,这里有个例外,那就是如果用户在Bean ...

  10. 升级完Android Studio3.2后,打包release出现的错误

    升级完Android Studio2.3后,打包release出现的错误 Error:Execution failed for task ':qq:lintVitalRelease'.> Lin ...