LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- Binary search tree or not
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- Python包,json&pickle,time&datetime,random模块
补充内容: 解决模块循环导入的两种方法:(不得已而为之,表示程序结构不够严谨) 将导入模块语句放在文件最下方 保证语句导入之前函数内代码能够被执行 将导入语句放进函数体内 使其不影响整个函数的运行 包 ...
- cocos2d-x游戏开发(二)之创建第一个项目
配置好开发环境之后,尝试创建一个cocos项目 (1)打开cocos2d-x安装目录,如D:\DIY\cocos2d-x-3.3 看到目录下有可执行文件 download-deps 以及 setup ...
- codeforces Lightsabers (hard)
题目大意: 给定每种球的数量,求从中选取k个球有多少种不同的取法,同种球视为相同的. 题解: 多项式(1+x+x^2+...+x^a[1])*(1+x+x^2+...+x^a[2])*(1+x+x^2 ...
- 【11】把 GitHub 当 CMS 用
把 GitHub 当 CMS 用 你的网站需要显示一些文字,但是你还不想直接放在 HTML 里面,那你可以把 GitHub 作为你储存内容的一个地方. 这样,就可以让任何一个非程序员通过修改 Mark ...
- php在字符串中替换多个字符
php替换多个字符串str_replace函数 echo str_replace(array("m","i"),array("n",&quo ...
- hdu2022
#include <stdio.h> #include <math.h> #define here puts("go,go,go!\n") int main ...
- MySQL5.7 MTS work线程stack
复制现象是,slave线程状态正常,但是sql 线程不应用,所以delay越来越大,查看复制状态 mysql> show slave status\G********************** ...
- 数组dome
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Terracotta服务器的不同配置方式
Terracotta服务器的不同配置方式 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 Terracotta双机多机镜像服务器阵列分片模式企业应用 Terracott ...
- iOS学习笔记21-推送证书与秘钥申请
---恢复内容开始--- 一.iOS证书 iOS常用的证书包括开发证书和发布证书,无论是真机调试还是最终发布应用到App Store这两个证书都是必须的,它是iOS开发的基本证书.Xcode7以后真机 ...