1. Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution

    Total Accepted: 68335 Total Submissions: 181124 Difficulty: Easy

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

    _______6______
/ \
___2__ ___8__

/ \ / \

0 _4 7 9

/ \

3 5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

本题是BST,是二叉查找树

针对二叉查找树BST的情况

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
assert(p!=NULL&&q!=NULL);
if(root==NULL) return NULL;
if(max(p->val, q->val) < root->val)
return lowestCommonAncestor(root->left, p, q);
else if(min(p->val, q->val) > root->val)
return lowestCommonAncestor(root->right, p, q);
else return root;
}
};

针对一般的树有以下解决方法:

思路:

判定是否根,是的话返回根,不是

判定p,q的分布,如下:

/**
* 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) {
assert(p!=NULL&&q!=NULL); //默认p,q不为空,否则为空的不知归为哪个节点
if(root==NULL)return NULL;
if(root==p||root==q)return root;
TreeNode *tmp;
bool right_p=inTheTree(root->right,p),left_p = inTheTree(root->left,p);
bool right_q=inTheTree(root->right,q),left_q = inTheTree(root->left,q);
if((right_p&&left_q)||(right_q&&left_p))return root; //分别在左右子树,返回根
if(right_p&&right_q)return lowestCommonAncestor(root->right,p,q);//全在右子树,转化为根为右节点的子问题
if(left_p&&left_q)return lowestCommonAncestor(root->left,p,q);//全在左子树,转化为根为右节点的子问题
return tmp;
}
bool inTheTree(TreeNode* head, TreeNode* p)//判定p是否在树中
{
if(head==NULL||p==NULL)return false;
if(head==p)return true;
else return inTheTree(head->left,p)||inTheTree(head->right,p);
}
};

60-Lowest Common Ancestor of a Binary Search Tree的更多相关文章

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

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

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

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

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

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

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

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

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

  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. populating-next-right-pointers-in-each-node leetcode C++

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  2. P2472 [SCOI2007]蜥蜴(最大流)

    P2472 [SCOI2007]蜥蜴 自己第一道独立做题且一遍AC的网络流题纪念... 看到这道题我就想到网络流建图的方式了... 首先根据每个高度,我们将每个点拆成两个点限流.之后根据跳的最大距离, ...

  3. hdu 5175 Misaki's Kiss again(GCD和异或)

    题意: 给一个数N. 如果GCD(N,M) = N XOR M,则称M是一个kiss   1<=M<=N 问总共有多少个kiss.并且列出所有的值. 思路: 思路一:枚举M.有大量的GCD ...

  4. 力扣 - 剑指 Offer 58 - I. 翻转单词顺序

    题目 剑指 Offer 58 - I. 翻转单词顺序 思路1 假如题目要求我们翻转字符串,那么我们可以从末尾往前开始遍历每一个字符,同时将每一个字符添加到临时空间,最后输出临时空间的数据就完成翻转了, ...

  5. Xtrabackup 全量备份脚本

    #!/bin/bash #备份文件的名字为当前主机的IP地址+tar.gz,例如172.16.103.1.tar.gz,且每次备份成功之后都会清空本地的备份目录. #相关目录 mkdir -p /xt ...

  6. vim 脚本,自动添加文件头部信息

    相信很多人编写脚本的时候都会在脚本头部写一些信息,记录文件生成时候,生成人姓名等 建议在自己的家目录下的 .vimrc 文件 下添加以下内容 [ autocmd BufNewFile *.sh exe ...

  7. 1.在项目中使用D3.js

    在项目中使用D3.js D3.js(全称:Data-Driven Documents)是一个基于数据操作文档的JavaScript库.D3帮助您使用HTML.SVG和CSS使数据生动起来.D3对web ...

  8. Python基础(range)

    arr = [1,2,3,4,5,6,7,8,9] for i in range(0,len(arr),2): print(arr[i],end=' | ') brr = arr[0:len(arr) ...

  9. Nginx支持WebSocket反向代理

    WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...

  10. python实现其它形态学操作

    目录: (一) 顶帽(原图像与开操作图像的差值)(二) 黑帽(原图像与闭操作图像的差值)(三) 形态学梯度  (1)基本梯度(膨胀后的图像与腐蚀后的图像差值)  (2)内部梯度(原图像减去腐蚀后的图像 ...