[LeetCode&Python] Problem 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.

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.
# 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, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
leaf=[] def findleaf(r):
if not r.left and not r.right:
leaf.append(r.val)
elif not r.left and r.right!=None:
findleaf(r.right)
elif not r.right and r.left!=None:
findleaf(r.left)
else:
findleaf(r.left)
findleaf(r.right) findleaf(root1)
leaf1=leaf
leaf=[]
findleaf(root2) if leaf==leaf1:
return True
return False
[LeetCode&Python] Problem 872. Leaf-Similar Trees的更多相关文章
- [LeetCode&Python] Problem 617. Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- 简说Spring事务
一.事务定义: 事务指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 二.事务的特性: 1. 原子性 - 指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 2. 一致性 ...
- 《剑指offer》第二十题(表示数值的字符串)
// 面试题20:表示数值的字符串 // 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如, // 字符串“+100”.“5e2”.“-123”.“3.1416”及“-1E-16 ...
- HDU 6114 Chess
Chess 思路:求C(n,m),除法取余用乘法逆元算. 代码: #include<bits/stdc++.h> using namespace std; #define ll long ...
- ln软连接
ln软连接 ln -s 源目录/文件 目标目录/文件 例如,有个应用 /var/www/html/webapp,下面有个logs日志文件夹,想吧 webapp/logs日志文件夹链到/home ...
- php 8小时时间差的解决方法小结
原来从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的 也就是显示的时间(无论用什么php命令)都是格林威治标准时间 和我们的时间(北京时间)差了正好 ...
- 3.4 复杂的x86指令举例
计算机组成 3 指令系统体系结构 3.4 复杂的x86指令举例 x86作为复杂指令系统的代表,自然会有不少相当复杂的指令.在这一节我们将会看到其中有代表性的一些例子. 关于复杂的x86指令,我们这里举 ...
- codeforces 993c//Careful Maneuvering// Codeforces Round #488 by NEAR (Div. 1)
题意:x轴-100和+100的有敌人飞船,纵坐标由输入数据给出,我方有2飞船在x轴0,y坐标待定.0时刻时敌人同时向我方2飞船发出光线,光线会穿透飞船打到敌人自己,问2飞船放在哪敌人损失最大? 假如- ...
- TCP客户端与服务器的实现
为了更容易理解,我们举一个小例子来说明服务器与客户端之间的连接过程. 有一个饭店,饭店里有服务员,服务员用于招待客人 特别要注意的是:要记住相关函数的各个参数都是什么,什么时候返回SOCKET_ERR ...
- zzuli 1430 多少个0
https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1430 1430: 多少个0 Time Limit: 1 Sec Memory Limit: 12 ...
- POJ服务器不能启动问题
问题1: 启动tomcat时出现错误:tomcat Address already in use: JVM_Bind:80 按照网上的方法,查找占用80端口的进程:netstat -ano 任务管理器 ...