题目要求

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 解题报告的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  10. 【LeetCode】838. Push Dominoes 解题报告(Python)

    [LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

随机推荐

  1. vs2015打开慢的解决方法

        1.首先是这里,这里默认是用的软件加速,把"基于客户端性能自动调整视觉体验"去掉勾选.然后把下面的第一个选项去掉,第二选项勾选.我在想,它的"自动"基于 ...

  2. Nginx配置WebService、MySQL、SQL Server、ORACLE等代理

    首先介绍一下Nginx的基本使用: 注意不要直接双击nginx.exe,这样会导致修改配置后重启.停止nginx无效,需要手动关闭任务管理器内的所有nginx进程 在nginx.exe目录,打开命令行 ...

  3. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  4. cp显示进度条

    cp显示进度条 alias cp='rsync -av --progress'

  5. Java如何查找系统的代理设置?

    在Java编程中,如何查找系统的代理设置? 以下示例显示如何使用HttpURLConnection类的systemSetting()方法和getResponse()方法的put方法在系统上查找代理设置 ...

  6. Tomcat容器做到自我保护,设置最大连接数(服务限流:tomcat请求数限制)

    http://itindex.net/detail/58707-%E5%81%87%E6%AD%BB-tomcat-%E5%AE%B9%E5%99%A8 为了确保服务不会被过多的http长连接压垮,我 ...

  7. 怎样利用Heartbeat与Floating IP在Ubuntu 14.04上创建高可用性设置

    提供 ZStack社区 内容简单介绍 Heartbeat是一款开源程序,负责将集群基础设施容量--包括集群成员与消息收发--交付至客户server. Hearbeat在高可用性server基础设施其中 ...

  8. sscanf的字符串格式化用法

    sscanf()为C语言标准库函数,用于从指定字符串中读入与指定格式相符的数据.函数原型声明在stdio.h头文件中: int sscanf(const char *str, const char * ...

  9. scala 模式匹配详解 2 scala里是怎么实现的?

    在这篇martin和另外两位模式匹配领域专家的论文里说了模式匹配的几种实现方式,以及scala是选择哪种方式来实现的.http://lampwww.epfl.ch/~emir/written/Matc ...

  10. 6.29一个_rcv 面试题

    #coding:utf-8 #2018-6-29 16:30:34 #类调用属性,属性没有,用__getatrr__魔法方法! #目的打印出 think different itcast class ...