LeetCode 872 Leaf-Similar Trees 解题报告
题目要求
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
题目分析及思路
题目给出二叉树的leaf-similar的定义,即要求两棵二叉树按照从左到右的顺序,它们的叶子节点值序列相同。可以定义一个函数,使用递归得到叶子节点值序列,相比遍历多添一个判断条件。
python代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def leafSimilar(self, root1: 'TreeNode', root2: 'TreeNode') -> 'bool':
def order(root):
res = []
if not root:
return res
if not root.left and not root.right:
res.append(root.val)
return res
res.extend(order(root.left))
res.extend(order(root.right))
return res
return order(root1) == order(root2)
LeetCode 872 Leaf-Similar Trees 解题报告的更多相关文章
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
随机推荐
- pandas 学习总结
pandas 学习总结 作者:csj 更新时间:2018.04.02 shenzhen email:59888745@qq.com home: http://www.cnblogs.com/csj0 ...
- 微信mac版的bug 直接显示了消息原始数据
<msg> <voicemsg endflag="1" cancelflag="0" forwardflag="0" vo ...
- VueThink配置
vuethink 配置 原文地址:http://blog.csdn.net/hero82748274/article/details/76100938
- 在windows下安装git后没有ssh文件夹
在windows7下安装git后,运行 cd ~/.ssh $ bash: cd: /c/Users/Administrator/.ssh: No such file or directory 出现以 ...
- 什么是对象:EVERYTHING IS OBJECT(万物皆对象)
所有的事物都有两个方面: 有什么(属性):用来描述对象. 能够做什么(方法):告诉外界对象有那些功能. 后者以前者为基础. 大的对象的属性也可以是一个对象.
- Mac 挂载树莓派nfs
vim /etc/export /data_sda1/ 192.168.1.*(rw,sync,insecure,no_root_squash) mac端自带rpcbind 挂载 sudo moun ...
- Android实现电话录音功能
需求分析 电话录音是在通话的时候进行录音,所以需要使用一个服务来完成功能. 需要监听电话的状态,分为三种状态: 空闲状态 TelephonyManager.CALL_STATE_IDLE 响铃状态 ...
- Service 中的 onStart 和 onStartCommand
在自定义的service中,写了onStart和onStartCommand, public class HttpWebService extends Service { @Override publ ...
- SVN 命令行的使用
大多数时候我们用TortoiseSVN作为客户端,其实SVN提供了强大的客户端命令行工具,和Git差不不多. 1. 查看工作副本修改的整体状况. $ svn status ? scratch.c A ...
- C++定义字符数组
问:C++中定义字符型数组时'\0'是不是也占一位?是不是定义char a[5],只能有4个字符?那计算字符长度时又否忽略'\0'? 答: C++中定义字符型数组时'\0'是不是也占一位?是不是定义c ...