236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
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).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5
and 1
is 3
. Another example is LCA of nodes 5
and 4
is 5
, since a node can be a descendant of itself according to the LCA definition.
===============
CLA问题,
利用递归方式:采用的后序遍历的思想,LRN
左子树查找,右子树查找,这个思路进行.
TreeNode *seek(TreeNode *root,TreeNode *p,TreeNode *q){
help_seek();
}
void help_seek(TreeNode *root, TreeNode *p,TreeNode *q,bool l,bool r){
if(r && l) cout<<"find the value""<<root->val<<endl;
if(root){
help_seek(root->left);
help_seek(root->right);
}
}
???????
====
代码:
/**
* 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==nullptr) return NULL;
if(root==p || root==q) return root;
TreeNode *left_lca = lowestCommonAncestor(root->left,p,q);
TreeNode *right_lca = lowestCommonAncestor(root->right,p,q);
if(left_lca&&right_lca) return root; return left_lca==nullptr? right_lca:left_lca;
}
};
236. Lowest Common Ancestor of a Binary Tree的更多相关文章
- 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 利用二 ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- Apache的虚拟主机配置
使用虚拟主机要先取消中心主机,注释掉DocumentRoot #DocumentRoot "/www/htdoc" 虚拟主机的单独配置: 用户认证 访问日志 错误日志 别名 脚本别 ...
- Linux软件安装管理概述
介绍如何在Linux字符界面下安装软件 课程大纲: 一.软件包管理简介 二.rpm命令管理 三.yum在线管理 四.源码包管理 五.脚本安装包
- Linux下串口与工业协议的开发
1.串口通信原理 串口通信定义 串口通信:数据的串行传送方式.串口通信可分为同步通信与异步通信. 同步通信:按照软件识别同步字符来实现数据的发送和接收. 将许多字符组成一个信息组进行发送 要求发送时钟 ...
- Java设计模式之简单工厂设计模式
简单工厂将业务逻辑部分和界面逻辑部分分离开来,降低了界面逻辑和业务逻辑的耦合度,符合面向对象迪米特法则.下面以一个加法减法运算器为例,各位读者可以自行按照这种设计方式设计出一个小小的运算器. 1.业务 ...
- GOOGLE的专业使用方法(转)
搜索引擎命令大全! 1.双引号 把搜索词放在双引号中,代表完全匹配搜索,也就是说搜索结果返回的页面包含双引号中出现的所有的词,连顺序也必须完全匹配.bd和Google 都支持这个指令.例如搜索: “s ...
- .NET GC Server-Background-GC
Garbage Collection and Performancehttps://msdn.microsoft.com/en-us/library/ee851764(v=vs.110).aspx h ...
- jquery循环延迟加载,用于在图片加载完成后再加载js
<html> <head> <script type="text/javascript" src="http://lib.sinaapp.c ...
- weblogic管理3 - 生产模式下免密码管理配置
admin server免密码配置 >1. 生产模式中admin root目录下是否存在security/boot.properties文件 [weblogic@11g AdminServer ...
- Entity Framework调用表值函数实现全文检索?
CREATE FUNCTION [dbo].[udf_BookContentSearch](@keywords NVARCHAR(1000))RETURNS @BookPageDetail TABLE ...
- AR、美颜、机器人:计算机视觉库几乎无所不在
最近日本推出的反美颜应用Primo可能让感到不胜惶恐.其实,这样反人类的应用,你也能写出,不过必须了解的一些技术,就是计算机视觉.目前,计算机视觉库包括FastCV.OpenCV.JavaCV等. 相 ...