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. iOS-Core Animation: 变换

    仿射变换 用 CGPoint 的每一列和 CGAffineTransform 矩阵的每一行对应元素相乘再求 和,就形成了一个新的 CGPoint 类型的结果.要解释一下图中显示的灰色元素, 为了能让矩 ...

  2. QQ音乐flac音乐转MP6格式怎样实现

    很多时候我们所下载的音乐格式都不是MP3格式的,用起来都是有局限性的,因为很多播放器都是支持MP3格式的.很多时候为了方便使用,我们就需要将不同的音乐格式转换为MP3格式的.如flac音乐转MP3的问 ...

  3. 洛谷P3952 时间复杂度【字符串】【模拟】

    题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...

  4. class FrameHandlerMono : public FrameHandlerBase

    单目视觉里程计流程图 class FrameHandlerMono : public FrameHandlerBase FrameHandlerMono::FrameHandlerMono(vk::A ...

  5. 静态方法中只允许访问静态数据,那么,如何在静态方法中访问类的实例成员(即没有附加static关键字的字段或方法)?

    package test.two; public class jingtaihanshu { int x = 3; static int  y = 4; public static void Meth ...

  6. post/get in console of JSarray/js 数组详细操作方法及解析合集

    https://juejin.im/post/5b0903b26fb9a07a9d70c7e0[ js 数组详细操作方法及解析合集 js array and for each https://blog ...

  7. linux系统操作笔记

    tar  cvf  test.tar  /etc gzip  test.tar bzep2 test.tar 归档压缩 tar czf  test.tar.gz  /etc vi  /etc/test ...

  8. [Day5]方法

    1.方法 (1)概念:方法就是用来完成解决某件事情或实现某个功能的办法 会包含很多条语句用于完成某些有意义的功能 通过在程序代码中引用方法名称和所需的参数,实现在该程序中执行(或称调用)该方法 (2) ...

  9. C++ Reflection Library

    C++ Reflection Library https://www.rttr.orghttps://github.com/rttrorg/rttr

  10. kubernetes的CI/CD

    部署流程:把编码上传到gitlab上,使用webhook链接jenkins自动去编译docker镜像,然后上传到harbor本地docker镜像库中,再自动下载docker镜像,使用k8s控制dock ...