[LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS
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
1and100nodes.
思路, 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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. ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- L - Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...
- 关于SQL SERVER中的FLOAT转换为VARCHAR
关于SQL SERVER中的FLOAT转换为VARCHAR 一个FLOAT型的字段,要转换为VARCHAR,可是小数点后面的都自动被删去了...后查得可以通过如下转换获得: SELECT CAST(C ...
- 电子产品使用感受之--Mac Mini 买了之后有什么用?-- 开发啊!
2019.01.29 更新 Mac Mini 2018这么强劲的性能,不用来做点儿什么真是可惜了. 如果只是用来看看Youtube视频,打开网页看看twitter什么的,那可真是巨大的浪费了. 因为这 ...
- python面向对象:类方法
类的方法包括以下几种: 构造方法 :__init__(self,) 析构方法 :__del__(self) 类方法@classmethod.实例方法.静态方法@staticmethod 一.构造方法 ...
- iOS知识点持续更新。。。
1.自动布局拉伸和压缩优先级 Autolayout中每个约束都有一个优先级,优先级的范围是1~1000.创建一个约束,默认的优先级最高是1000. Content Hugging Priority:该 ...
- 2012年蓝桥杯省赛A组c++第3题(喝断片的海盗)
/* 有一群海盗(不多于20人),在船上比拼酒量.过程如下:打开一瓶酒, 所有在场的人平分喝下,有几个人倒下了.再打开一瓶酒平分,又有倒下的, 再次重复...... 直到开了第4瓶酒,坐着的已经所剩无 ...
- DateTimePicker用法
将DateTimePicker的Format属性中加入日期格式设成 'yyyy-MM-dd HH:mm',注意大小写 , 将kind设置为dtkTime即可,可以在每次Form onShow时将Dat ...
- LeetCode 669 Trim a Binary Search Tree 解题报告
题目要求 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so t ...
- LeetCode 617 Merge Two Binary Trees 解题报告
题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- LeetCode 657 Robot Return to Origin 解题报告
题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...