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.
# 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Java之美[从菜鸟到高手演变]系列之博文阅读导航

    随着博文越来越多,为博客添加一个导航很有必要!本博客将相继开通Java.CloudFoundry.Linux.Ruby等专栏,都会设立目录,希望读者朋友们能更加方便的阅读! 在阅读的过程中有任何问题, ...

  2. Codeforces 534B - Covered Path

    534B - Covered Path 思路:贪心,每一秒取尽可能大并且可以达到的速度. 画张图吧,不解释了: 代码: #include<bits/stdc++.h> using name ...

  3. 关于怎么解决java.lang.NoClassDefFoundError错误

    五一在部署新的统一登录时,遇到这样一个问题: 很容易把java.lang.NoClassDefFoundError和java.lang.ClassNotfoundException这两个错误搞混,事实 ...

  4. SVG基础图形和D3.js

    使用D3.js画一个SVG 的 圆 circle 可以使用如下代码创建: <svg width="50" height="50"> <circ ...

  5. Longest Repeating Character Replacement

    2018-06-29 22:56:24 问题描述: 问题求解: 本题是一条字符串问题,且是求Optimal,自然最初想到的是使用DP来进行求解,但是问题就是如果采用DP的话,前一个状态也太多了,和替换 ...

  6. 雷林鹏分享:C# 异常处理

    C# 异常处理 异常是在程序执行期间出现的问题.C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零. 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式.C# 异常处理时建立 ...

  7. 用Rails.5.2+ Vue.js做 vue-todolist app

    Rails5.2+Vue.js完成Lists(curd) 注意: Edit/update使用SPA(single-page Application单页面程序)的方法完成.点击文字出现一个输入框和按钮. ...

  8. spoj The Next Palindrome

    题意:比给出的数大的最小回文数. 先用前n/2长对称到后面,如果没变大,在中间加1,进位,再对称. //#pragma comment(linker,"/STACK:1024000000,1 ...

  9. codeforces 559a//Gerald's Hexagon// Codeforces Round #313(Div. 1)

    题意:面积是sqrt(3)/4的多少倍? 做延长线 #pragma comment(linker,"/STACK:1024000000,1024000000") #include& ...

  10. PHP函数总结 (七)

    <?php /** * 匿名函数(闭包函数): * php>=5.3 * 允许临时创建一个没有指定名称的函数,常作为回调函数参数的值 * * 闭包的另一个概念: * 在内部函数中可以使用外 ...