解法一:递归

 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
if (root == NULL || p == NULL || q == NULL)
return NULL; 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);
return root;
}

解法二:迭代

 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
if (root == NULL || p == NULL || q == NULL)
return NULL; while (true) {
if (root->val < p->val && root->val < q->val)
root = root->right;
else if (root->val > p->val && root->val > q->val)
root = root->left;
else
break;
}
return root;
}

【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  2. 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 利用二 ...

  3. 【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 ...

  4. LeetCode_235. Lowest Common Ancestor of a Binary Search Tree

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

  5. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  6. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...

  7. 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

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

  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] 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 ...

  10. [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. UVa 1610 聚会游戏

    https://vjudge.net/problem/UVA-1610 题意:输入一个n个字符串的集合D,找一个长度最短的字符串S,使得D中恰好有一半串小于等于S,另一半串大于S. 思路:先拍序,然后 ...

  2. go 修改字符串

    在Go中字符串是不可变的,例如下面的代码编译时会报错:cannot assign to s[] var s string = "hello" s[] = 'c' 但如果真的想要修改 ...

  3. c++ 判断给定区间是否是一个heap. O(N) (is_heap)

    #include <iostream> // cout #include <algorithm> // is_heap, make_heap, pop_heap #includ ...

  4. 【转】Qt在pro中设置运行时库MT、MTd、MD、MDd,只适合VS版本的Qt

    http://blog.csdn.net/caoshangpa/article/details/51416077 一.在pro中设置运行时库 最近在用Qt5.6.0(VS2013版本)调用一份用Vis ...

  5. JavaScript算法相关

    1. 不使用循环,创建一个长度为100的数组,并且每个元素的值等于它的下标? Array.apply(null, {length: N}).map(Function.call, Number); Ar ...

  6. HDU 4054 Number String

    HDU 4054 Number String 思路: 状态:dp[i][j]表示以j结尾i的排列 状态转移: 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + ...

  7. 雷林鹏分享:Ruby 块

    Ruby 块 您已经知道 Ruby 如何定义方法以及您如何调用方法.类似地,Ruby 有一个块的概念. 块由大量的代码组成. 您需要给块取个名称. 块中的代码总是包含在大括号 {} 内. 块总是从与其 ...

  8. (转)HapMap简介

    1.人类基因组的HapMap和国际HapMap计划 (1)何谓HapMap HapMap是Haplotype Map 的简称,Haplo意为单一,在基因组中专指来自父母的一对染色体中的一条.Haplo ...

  9. 问题✅:render json的格式支持。to_json被改成as_json,功能一样

    class StudentsController < ApplicationController def show @student = Student.find(params[:id]) re ...

  10. php项目------浏览器导航开发

    最近项目不是很急,把以前做的php项目分享一些给大家,希望对各位有所帮助:很简单的一个项目,本人用来练习ThinPHP框架的. 浏览器导航开发,php+mysql+apache,ThinkPHP3.2 ...