【Lowest Common Ancestor of a Binary Tree】cpp
题目:
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.
代码:
/**
* 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 || root==p || root==q ) return root;
TreeNode* l = Solution::lowestCommonAncestor(root->left, p, q);
TreeNode* r = Solution::lowestCommonAncestor(root->right, p, q);
if ( l && r )
{
return root;
}
else
{
return l ? l : r;
}
}
};
tips:
直接学习大神的思路(http://bookshadow.com/weblog/2015/07/13/leetcode-lowest-common-ancestor-binary-tree/),确实很巧妙。
总体来说,就是分叉找;终止条件是到了叶子或者找到p和q的一个就返回了。
这里有个思维误区,为啥找到p或q的一个就返回了,如果先找到p而q在p的下面呢?那就正好了,反正这俩点如果q在p的下面,那么根据定义p就是p和q的公共祖先。
【Lowest Common Ancestor of a Binary Tree】cpp的更多相关文章
- 【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 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 ...
 - 88 Lowest Common Ancestor of a Binary Tree
		
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
 - 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - [LeetCode] 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 Lowest Common Ancestor of a Binary Tree
		
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
 - [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 ...
 
随机推荐
- MySQL入门很简单:  15 java访问MySQL数据库
			
1. 连接数据库 1.1 下载安装驱动 java通过JDBC(Java Database Connectivity,Java数据库连接)来访问MySQL数据库.JDBC的编程接口提供的接口和类与MyS ...
 - vue.js--基础 v-bind绑定属性使用
			
背景:因为10月要休产假了,8月的时间我工作很少,因为最开始做平台我一直做的是后端,前端很少接触,所以现在有时间,就学习前端基础,前端使用的vue.js+element,因为没有基础,所以下了一个视频 ...
 - 2017.11.4  JavaWeb-----基于JavaBean+JSP求任意两数代数和(改进的在JSP页面中无JSP脚本代码的)+网页计数器JavaBean的设计与使用
			
修改后的JSP中不含有JSP脚本代码这使得JSP程序的清晰性.简单 1.设计JavaBean 的Add.java 类 package beans; public class Add { private ...
 - 2017.10.14   Java的流程控制语句switch&&随机点名器
			
今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作 ...
 - git 提交  src refspec master does not match any
			
git init 产生的目录解释 error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error ...
 - 不使用data-ng-app指令的表达式
			
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
 - JDBC完美连接方法
			
jdbc:mysql://localhost:3306:test这句里面分如下解析:jdbc:mysql:// 是指JDBC连接方式:localhost: 是指你的本机地址:3306 SQL数据库的端 ...
 - JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库
			
简介 项目主页: https://github.com/bang590/JSPatch 示例下载: https://github.com/ios122/ios122 JSPatch 可以让你用 Jav ...
 - Navicat Premium Mac 12 破解
			
破解地址:https://blog.csdn.net/xhd731568849/article/details/79751188 亲测有效
 - MySQL备份工具percona-xtrabackup安装
			
1.安装xtrabackup的yum源 rpm -ivh https://www.percona.com/redir/downloads/percona-release/redhat/latest/p ...