题意大概是,找两个节点的最低公共祖先

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL)
return NULL;
if (root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left,p,q);
if (root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right,p,q);
return root; //出现了一大一小,就可以返回root了
}
};

【easy】235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

    Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...

  2. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  3. 【LeetCode】235. 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 th ...

  4. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. (easy)LeetCode 235.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 ...

  6. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  8. [LeetCode] 235. 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] 235. 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. 普通PC通过USB转485串口 ModBus-RTU通信协议控制伺服电机

    一.RS485通信 RS485 是半双工通信(2 线制),可以一点对多点进行组网,而且 RS485 是用缆线两端的电压差值来表示传递信号,这与 RS232 电气特性大不一样.RS485 仅仅规定了接收 ...

  2. SQL操作符、通配符等

    一.通配符 常用模糊查询:% SELECT * FROM TB_Name WHERE FIELD LIKE pattern   SELECT * FROM Persons WHERE name LIK ...

  3. 使用redis可能出现的问题

    1)缓存与数据库双写不一致 2)缓存雪崩 3)缓存穿透 由于缓存中不存在某个key,所有的请求都会落到数据库上,会对数据库造成很大压力,甚至崩溃 一个简单的方案是将不存在的数据也缓存起来(value值 ...

  4. jdk自带的监测cpu/内存、线程等信息的工具

    Jvisualvm:jdk自带的监控工具(JDK1.6+) 在终端中输入Jvisualvm回车出现如下界面:

  5. jQuery 与 Ajax 的应用

    Ajax 全称为 "Asynchronous JavaScript and XML"(异步 JavaScript 和 XML ),它并不是指一种单一的技术,而是有机地利用了一系列交 ...

  6. centos6.5之phpmyadmin安装

    PhpMyAdmin 首先我们看一下百度百科,看一下phpmyadmin是做什么的. phpMyAdmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让 ...

  7. codeforces675D

    Tree Construction CodeForces - 675D During the programming classes Vasya was assigned a difficult pr ...

  8. [BJOI2017]树的难题

    题目描述 给你一棵 n 个点的无根树. 树上的每条边具有颜色.一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci. 对于一条树上的简单路径,路径上经过的所有边按顺序组成一个颜色序列, ...

  9. visp库中解决lapack库的问题

    解决的办法是——绕过去,不要用这个库: 使用中发现如下代码抛出异常: //vpTemplateTracker.cpp try { initHessienDesired(I); ptTemplateSu ...

  10. BUG in Ubuntu--Could not get lock /var/lib/dpkg/lock

    在ubuntu中通过apt安装软件时,报错: E: Could not : Resource temporarily unavailable) E: Unable to lock the admini ...