自己的代码:

# 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. JavaScript standard 代码规范的全文

    这是 JavaScript standard 代码规范的全文. 掌握本规范的最好方法是安装并在自己的代码中使用它. 细则 使用两个空格进行缩进. eslint: indent function hel ...

  2. ArcGis实现画矩形(RectangleFeedBack)

    private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)         ...

  3. View的setTag和getTag方法

    ---恢复内容开始--- public View getView(int position, View convertView, ViewGroup parent) { Msg msg =getIte ...

  4. C#Owin auth20开发 OwinStartup 不会触发的解决办法

    在使用owin auth20设置token时候遇到一个问题.项目中已经存在如下初始化配置类 using Microsoft.Owin; using Owin; [assembly: OwinStart ...

  5. 用JS实现的常见几种排序算法

    1.快速排序法 function quickSort(a) { if (a.length <= 1) { return a; } var midLength = Math.floor(a.len ...

  6. 打开struts-config.xml 报错 解决方法Could not open the editor

    打开struts-config.xml 报错 解决办法Could not open the editor 错误信息:Could not open the editor: Project XXX is ...

  7. oracle动态添加一条记录

    /// <summary> /// 添加一个实体 /// </summary> /// <typeparam name="T">实体名称< ...

  8. Tomcat6.0下的jsp、servlet和javabean的配置

    第一步:下载jdk和tomcat: 第二步:安装和配置你的jdk和tomcat:执行jdk和tomcat的安装程序,然后设置按照路径进行安装即可.1.安装jdk以后,需要配置一下环境变量,在我的电脑- ...

  9. jQuery 中bind(),live(),delegate(),on() 区别

    on()来改写通过 .bind(), .live(), .delegate()所注册的事件 /* The jQuery .bind(), .live(), and .delegate() method ...

  10. swift版的枚举变量

    swift版的枚举变量 swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码 // // ViewController.swift // SwiftEnum ...