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. 【转】数据仓库ODS、DW和DM概念区分

    今天看了一些专业的解释,还是对ODS.DW和DM认识不深刻,下班后花时间分别查了查它们的概念. ODS——操作性数据 DW——数据仓库 DM——数据集市 1.数据中心整体架构   数据中心整体架构 数 ...

  2. 图像分割loss集合

    我们只是大佬的搬运工 1.log loss 2.WBE loss 带权重的交叉熵 3.Focal loss 容易过拟合?我在VGG16上做过实验(没有BN层),发现网络在训练集上的性能直线上升,但是验 ...

  3. 入门人工智能的首选语言为什么会是Python?

    为何人工智能(AI)首选Python?当你读完这篇文章就会明白了.为何人工智能(AI)首选Python?读完这篇文章你就知道了.我们看谷歌的TensorFlow基本上所有的代码都是C++和Python ...

  4. 爬虫练习四:爬取b站番剧字幕

    由于个人经常在空闲时间在b站看些小视频欢乐一下,这次就想到了爬取b站视频的弹幕. 这里就以番剧<我的妹妹不可能那么可爱>第一季为例,抓取这一番剧每一话对应的弹幕. 1. 分析页面 这部番剧 ...

  5. Objective-C 正则表达式使用(1)

    学习了一下OC的正则表达式备忘一下 使用正则表达式的步骤: 创建一个一个正则表达式对象:定义规则. 利用正则表达式对象测试,相应的字符串. NSString *userName = @"12 ...

  6. I2C驱动框架(五)

    参考:I2C子系统之 adapter driver注册——I2C_dev_init() i2c的操作在内核中是当做字符设备来操作的,相关初始化在由i2c_dev_init函数来初始化. static ...

  7. Linux磁盘与文件系统管理 之 认识EXT2系统

    1 磁盘组成与分区 1.1 磁盘物理组成 (1)圆形盘片-记录数据 (2)机械手臂及磁头-读写盘片数据 (3)主轴马达-使得机械手臂成功读写数据驱动 1.2 盘片物理组成 (1)扇区-最小物理存储单位 ...

  8. PHP-redis命令之 字符串 (strings)

    一.string (字符串) 1.set:设置键 $reids->set('mykey',111); 2.get:获取键 $redis->get('mykey'); 3.del:删除键 $ ...

  9. app启动画面(prepo)

    IPhone启动画面以及图标的设置 目前IPhone的分辨率为:320X480.640X960.640X1136. Default.png                    320X480 iPh ...

  10. 面试准备——rpc面试题

    https://www.jianshu.com/p/28e48e5f9c73 1 什么是 RPC ? RPC (Remote Procedure Call)即远程过程调用,是分布式系统常见的一种通信方 ...