Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

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.

Note:

  • Both of the given trees will have between 1 and 100 nodes.

思路, DFS, 将所有leaves从左到右append进入leaves里面. 最后比较.

T: O(n1 + n2),    S: O(max(n1,n2))

Code

class Solution:
def leafSimilarTree(self, root1, root2):
def dfs(root, leaves):
if not root: return
if not root.left and not root.right:
leaves.append(root.val)
dfs(root.left, leaves)
dfs(root.right, leaves)
return dfs(root1, []) == dfs(root2, [])

[LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS的更多相关文章

  1. [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  2. [LeetCode] 110. Balanced Binary Tree_Easy tag: DFS

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  4. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  5. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  6. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  9. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

随机推荐

  1. day_5.27python网络编程

    开始进行python网络编程2018-5-27 20:27:30 Tcp/Ip协议

  2. freemarker 中可以直接使用的内置对象

    freemarker 中可以直接使用的内置对象 需要配置一下:springboot中配置 ## Freemarker \u914D\u7F6E ## \u6587\u4EF6\u914D\u7F6E\ ...

  3. fclose函数无响应

    现象:win32程序在退出时无响应,当一步步跟踪代码时走到fclose. 原因:打开文件在一个线程中,写文件时在另一个线程,在open和write文件时均正常,只有在fclose时出现无响应. 解决: ...

  4. Adobe Photoshop CS6简单的破解

    由于网站的页面布局和素材准备等等需要用到Photoshop,所以下载了个 Photoshop CS6,写这份破解文档的大佬感觉写的很复杂,看了让人头疼,乱搞中突然发现一个方法可以很快的进行破解操作,我 ...

  5. c++基本函数

    std::abs 求绝对值函数 double abs (double x); float abs (float x); long double abs (long double x); std::sw ...

  6. 用homebrew 升级安装python3.7 之后系统的python版本还是旧的怎么办

    mac 中安装了多个版本的python$ brew install python3 Updating Homebrew... Warning: python is already installed, ...

  7. Flannel - 原理

    Flannel GitHub 地址 Flannel 是 Kubernetes 中常用的网络配置工具,用于配置第三层(网络层)网络结构. 如何工作Flannel 需要在集群中的每台主机上运行一个名为 f ...

  8. 拳打Adam,脚踢SGD:北大提出全新优化算法AdaBound

    https://mp.weixin.qq.com/s/el1E-61YjLkhFd6AgFUc7w

  9. __dict__和dir()的区别:未完

    1.  dir()是一个函数,返回的是list.__dict__是一个字典,键为属性名,值为属性值: 2.  dir()用来寻找一个对象的所有属性,包括__dict__中的属性,所以说__dict__ ...

  10. 转:cookie.setPath()用法

    原文地址:cookie.setPath()的用法 正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得. 1.可在同一应用服务器内共享方法:设置cookie.setPath ...