[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 ...
随机推荐
- 03_ExeZZ.cpp
1.静态加载 DLL : #pragma comment(lib, "DllZZ.lib") __declspec(dllimport) void __stdcall AA(); ...
- 雷林鹏分享:C# 异常处理
C# 异常处理 异常是在程序执行期间出现的问题.C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零. 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式.C# 异常处理时建立 ...
- Java类和对象的概念
Java是一门面向对象的编程语言,理解Java,首先要理解类与对象这两个概念. Java中的类可以看做C语言中结构体的升级版.结构体是一种构造数据类型,可以包含不同的成员(变量),每个成员的数据类型可 ...
- (转)sublime text3 3176激活
更改hosts:sudo vim /private/etc/hosts 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 激活 ...
- Android之EventBus1.0 和EventBus3.0的使用详解
当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,那么我们通常采用的就是Android中的解耦组件EventBus.EventBus是一款针对Android优化的发布/订阅 ...
- Confluence 6 从外部目录中同步数据手动同步缓存
你可以通过单击用户目录(User Directories)界面中的同步(Synchronize)按钮,手动进行同步.如果一个同步进程已经正在同步的过程中的话,你就不能在上一个同步进程完成之前重新进行同 ...
- Cloud Commander
一.Cloud Commander简介: Cloud Commander 是一个基于 web 的文件管理程序,它允许你通过任何计算机.移动端或平板电脑的浏览器查看.访问或管理系统文件或文件夹.它有两个 ...
- ccf行车路线
#include<bits/stdc++.h> using namespace std; const long long INF = 1e18; int n,m; struct node{ ...
- hdu 1258
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Leetcode 90
// 重复元素在去重的时候会出现顺序不同去不了重,这时候需要对add进行排序class Solution { public: vector<vector<int>> subse ...