Leetcode ——Lowest Common Ancestor of a Binary Tree
Question
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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
_______3______
/ \
___5__ ___1__
/ \ /
6 _2 0 8
/
7 4
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of of nodes 5 and 1 is 3.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself
according to the LCA definition.
Note:
All of the nodes' values will be unique.
p and q are different and both values will exist in the binary tree.
Solution
递归。当前根节点root,其左右子树分别包含要找的节点,那么当前root,就是他们的公共祖先。
Code
/**
* 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 || root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
return left == NULL ? right : right == NULL ? left : root;
}
};
Leetcode ——Lowest Common Ancestor of a Binary Tree的更多相关文章
- [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——Lowest Common Ancestor of a Binary Tree
题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- [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 ...
- 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 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
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 ...
随机推荐
- grunt学习二
1. 新建文件和文件目录 mkdir grunt-in-action cd grunt-in-action cd grunt-in-action mkdir grunt-empty cd grunt- ...
- 菜鸟教程之工具使用——Git的基本使用
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/liushuijinger/article/details/37569907 Git是进来比較火的版本 ...
- PHP 常用命令行
1.PHP运行指定文件 php -f test.php (-f 可省略) 2.命令行直接运行PHP代码 php -r "phpinfo();" 如果结果太长,还可以 php -r ...
- ubuntu 下执行定时任务
Window shell文件在linux系统下执行不了的解决办法 一些人喜欢用vim来写linux shell script, 但是, 有的人喜欢在Windows下用一些方便的编辑器(比如鼎鼎大名的N ...
- Kylin安装部署
一.安装准备 1.操作系统 Centos 7.x 2.时间问题 集群内所有节点时间一定要同步. NTP.Chrony 3.用户 创建hadoop组和hadoop用户,并做ssh免密码登录 4.Hado ...
- easyUI的datebox添加清空按钮功能
需要修改源码: 第一步:按下图修改 第二步:按下两图修改(*zh_CN.js)
- Tesseract-OCR 训练过程 V3.02
软件: jTessBoxEditor Version 0.9 (30 April 2013) Tesseract-OCR win32 v3.02 with Leptonica 训练步骤: 1. ...
- 处理hash冲突
“处理冲突” 的实际含义是: 为产生冲突的地址寻找下一个哈希地址. 1. 开放定址法 2. 链地址法 ------------------------------------------------- ...
- iOS9 & iOS10 & iOS11 HTTP 不能正常使用的解决办法
iOS9 & iOS10 & iOS11 HTTP 不能正常使用的解决办法 xcode ios 291.4k 次阅读 · 读完需要 8 分钟 54 今天升级Xcode 7.0 b ...
- Ajax棵
ajax 1.什么是ajax?(异步请求,局部刷新) ajax是一个改善用户体验的技术,实质上是利用浏览器端ajax对象()向服务器发送异步(ajax对象在向服务器发送请求的时候,用户可以继续其他操作 ...