【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
题目描述
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]
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.
题目大意
在一棵普通的二叉树中,找出两个节点的最小公共祖先。
解题方法
如果是BST的话,那就很简单了。可以参考一下【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)的做法。
最低公共祖先的定义是,在一个二叉树中,我们能找到的最靠近叶子的节点,该节点同时是p和q的祖先节点。注意,如果p或者q本身也可以作为自己的祖先。
如果用递归的话,最重要的还是明白递归函数的作用是什么。这个题里面lowestCommonAncestor(root, p, q)函数的作用是判断p和q在root树中最低的公共祖先是什么,返回值是公共祖先。
这个题的模式叫做devide and conquer. 如果当前节点等于其中的p和q某一个节点,那么找到了节点,返回该节点,否则在左右子树分别寻找。
左右子树两个返回的是什么呢?按照该递归函数的定义,即找到了左子树和右子树里p和q的公共祖先,注意祖先可以是节点自己。然后根据左右侧找到的节点做进一步的判断。
如果左右侧查找的结果都不为空,说明分别找到了p和q,那么LCA就是当前节点。否则就在不为空的那个结果就是所求。
python代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if not root or p == root or q == root:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
return left if left else right
日期
2018 年 6 月 22 日 —— 这周的糟心事终于完了
2019 年 1 月 9 日 —— 时不我待
【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)的更多相关文章
- [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(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 ...
- (medium)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. Accordi ...
- [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 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree
解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...
随机推荐
- 使用Openmp并行化
运行命令:g++ -fopenmp xx.cpp -lgomp -lpthread -o xx.out 用例一: #include <omp.h> #include <stdio.h ...
- C#表格GridView显示2位百分比
<asp:BoundField HeaderText="占比" DataField="number" DataFormatString="{0: ...
- 巩固javaweb第十八天
提交按钮 只要涉及提交信息,都应该提供一个提交按钮,当点击提交按钮的时候,用户输入的 信息将提交给服务器,意味着输入过程的结束.注册界面中也包含一个提交按钮. 提交按钮的基本格式如下: <inp ...
- day01互联网架构理论
- Linux系统中安装软件方法总结
Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...
- linux之wc命令详解
Linux系统中wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式 wc [options] 文件... 2.命令功能 统计指定文件中的字 ...
- 最新的Android Sdk 使用Ant多渠道批量打包
实例工程.所需的文件都在最后的附件中. 今天花费了几个小时,参考网上的资料,期间遇到了好几个问题, 终于实现了使用Ant批量多渠道打包,现在,梳理一下思路,总结使用Ant批量多渠道打包的方法:1 ...
- Vue.js 学习
一,Vue.js 介绍 Vue 是一套用于构建用户界面的渐进式javascript框架,与其它大型框架不同的是:Vue被设计为可以自底向上逐层应用.Vue的核心库只关注视图层,不仅易于上手,还便于与第 ...
- notepad++ 连接远程服务器
前言:为了便于编辑 linux 上的文件,因此通过 notepad++ 连接服务器后打开,编辑完,保存即可 1. 打开 notepad++,安装插件 2. 搜索 NppFtp,找到后 点击 安装/in ...
- 【C/C++】最长公共子序列(LCS)/动态规划
晴神这个的最巧妙之处,在于用dp[i][0] = dp[0][j] = 0的边界条件 这样从1的下标开始填数组的时候,递推公式dp[i-1][j-1]之类的不会报错 #include <iost ...