自己的代码:

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution: def allNode(self,root):
listNode=[]
if Not root:
return ListNode
checkResult=checkNode(root)
if checkResult is not None:
listNode.append(checkResult)
if not root.left:
self.allNode(root.left)
if not root.left:
self.allNode(root.right) def checkNode(self,root):
if not root.left and not root.right :
return root.val
return None def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
node1=allNode(root1)
node2=allNode(root2)
if node1==node2:
return True
return False

主要问题:思维有些混乱,在使用递归的时候,先把针对单个结点的返回想好,再使用递归。

优秀代码:

https://blog.csdn.net/fuxuemingzhu/article/details/81748617

先序遍历:

# 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
"""
return self.getLeafs(root1) == self.getLeafs(root2) def getLeafs(self, root):
res = []
if not root:
return res
if not root.left and not root.right:
return [root.val]
res.extend(self.getLeafs(root.left))
res.extend(self.getLeafs(root.right))
return res

修改之后的代码:

# 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):
node1=self.allNode(root1)
node2=self.allNode(root2)
if node1==node2:
return True
return False def allNode(self,root):
listNode=[]
if not root:
return ListNode
if not root.left and not root.right :
return [root.val]
listNode.extend(self.allNode(root.left))
listNode.extend(self.allNode(root.right))
return listNode

LeetCode872. Leaf-Similar Trees的更多相关文章

  1. 壁虎书6 Decision Trees

    Decision Trees are versatile Machine Learning algorithms that can perform both classification and re ...

  2. sql是如何执行一个查询的!

    引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQ ...

  3. Understanding how SQL Server executes a query

    https://www.codeproject.com/Articles/630346/Understanding-how-SQL-Server-executes-a-query https://ww ...

  4. Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black

    A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...

  5. [Swift]LeetCode872. 叶子相似的树 | Leaf-Similar Trees

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  6. Leetcode872.Leaf-Similar Trees叶子相似的树

    请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两颗二叉树的叶值序列是相同 ...

  7. Machine Learning Methods: Decision trees and forests

    Machine Learning Methods: Decision trees and forests This post contains our crib notes on the basics ...

  8. Gradient Boosted Regression Trees 2

    Gradient Boosted Regression Trees 2   Regularization GBRT provide three knobs to control overfitting ...

  9. Chp4: Trees and Graphs

    1.Type of Tree 1. Binary Tree: a binary tree is a tree in which each node has at most two child node ...

随机推荐

  1. 移除button点击时的黑边

    input[type=submit], input[type=reset], input[type=button]{ outline:none; filter: chroma(color=#00000 ...

  2. js-权威指南学习笔记5

    第六章 对象 1.对象的方法通常是继承的属性.这种原型式继承是JS的核心特征. 2.除了名字和值之外,每个属性还有一些与之相关的值,称为属性特性——可写/可枚举/可配置.数据属性的四个特性——值.可写 ...

  3. Flare-On4 解题复现

    01 是一个 html 页面, 用开发者工具看看,发现是简单的 js 加密. 猜测加密算法可逆,试着用 PyvragFvqrYbtvafNerRnfl@syner-ba.pbz 作为输入,然后调试 , ...

  4. Android内核漏洞利用技术实战:环境搭建&栈溢出实战

    前言 Android的内核采用的是 Linux 内核,所以在Android内核中进行漏洞利用其实和在 一般的 x86平台下的 linux 内核中进行利用差不多.主要区别在于 Android 下使用的是 ...

  5. FileTable的创建和使用

    1. 首先要在实例级别启用FILESTREAM,打开Sql Server Configuration Manager窗口,双击需要设置的SQL SERVER实例进行设置. 2. 需要更改 SQL SE ...

  6. 使用mac版思维导图软件MindNode

    下载地址 http://pan.baidu.com/s/1hq3fUVq 思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的 ...

  7. UIWebView如何加载本地图片

    UIWebView如何加载本地图片 UIWebView加载本地图片是有实用价值的.比方说,有时候我们需要本地加载静态页来显示相关帮助信息,而这些帮助信息当中含有很多很多的富文本,用代码实现难度较大,这 ...

  8. [控件] 动态实时设置CAShapeLayer贝塞尔曲线的坐标点

    动态实时设置CAShapeLayer贝塞尔曲线的坐标点 效果图: 源码: PathDirectionView.h 与 PathDirectionView.m // // PathDirectionVi ...

  9. [翻译] PJR Signature View

    PJR Signature View https://github.com/paritsohraval100/PJRSignatureDemo It is a UIView subclass by w ...

  10. Linux ss命令详解

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息 ...