自己的代码:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def allNode(self,root):listNode=[]if Not root:return ListNodecheckResult=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.valreturn Nonedef leafSimilar(self, root1, root2):""":type root1: TreeNode:type root2: TreeNode:rtype: bool"""node1=allNode(root1)node2=allNode(root2)if node1==node2:return Truereturn 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 = Noneclass 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 resif 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 = Noneclass Solution:def leafSimilar(self, root1, root2):node1=self.allNode(root1)node2=self.allNode(root2)if node1==node2:return Truereturn Falsedef allNode(self,root):listNode=[]if not root:return ListNodeif not root.left and not root.right :return [root.val]listNode.extend(self.allNode(root.left))listNode.extend(self.allNode(root.right))return listNode

转载于:https://www.cnblogs.com/captain-dl/p/10154334.html

LeetCode872. Leaf-Similar Trees相关推荐

  1. STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树

    STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树 最近在做项目时发现一个crash的问题,当时得到的dmp文件显示crash在一个以map为循环变量的循环中,crash位置在如下 ...

  2. 机器学习论文:《LightGBM: A Highly Efficient Gradient Boosting Decision Tree》

    翻译自<LightGBM: A Highly Efficient Gradient Boosting Decision Tree> 摘要 Gradient Boosting Decisio ...

  3. leetcode310. Minimum Height Trees

    题目 For an undirected graph with tree characteristics, we can choose any node as the root. The result ...

  4. Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

    D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...

  5. Gradient Boosting, Decision Trees and XGBoost with CUDA ——GPU加速5-6倍

    xgboost的可以参考:https://xgboost.readthedocs.io/en/latest/gpu/index.html 整体看加速5-6倍的样子. Gradient Boosting ...

  6. 机器学习算法 --- Decision Trees Algorithms

    一.Decision Trees Agorithms的简介 决策树算法(Decision Trees Agorithms),是如今最流行的机器学习算法之一,它即能做分类又做回归(不像之前介绍的其他学习 ...

  7. 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)...

    8.1 Trees -->root,children, parent, siblings, leaf; level, degree of element 的基本概念 8.2 Binary Tre ...

  8. leetcode讲解--872. Leaf-Similar Trees

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

  9. leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)

    题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...

最新文章

  1. Hyper-V 网卡 load failed
  2. angularjs与java_关于angularjs与java结合,获取后台数据并解析的问题
  3. 【DP】滑雪场的缆车(jzoj 1257)
  4. mysql select 返回列_SQL / mysql – selectdistinct / UNIQUE,但返回所有列?
  5. c#反混淆工具de4dot 一般混淆都可以解决
  6. Python GUI 编程
  7. 柱状图多维条形图vue_使用D3.js+Vue实现一个简单的柱形图
  8. 内网通v3.1.2141无捆绑绿色官方版
  9. 在线摩尔斯密码加密解密工具
  10. 用Python实现多国语言文章及URL网页在线自动翻译
  11. 运放的转换速率(压摆率)SR的意义和如何选取
  12. java程序设计概念对象先行_Java程序设计概念:对象先行(原书第8版)简介,目录书摘...
  13. Python基础(编码格式、文件、目录)
  14. 【unity地编】unity制作场景的流程和要点简要
  15. python个人数据脱敏_数据脱敏的处理方法及查询
  16. iPad协议接口-稳定版
  17. 图片站选择什么样的服务器好?
  18. 运动模糊运动拖影果冻效应分析
  19. C语言无符号数赋值为负数时的%d%u输出问题解释
  20. 无线路由器接入局域网的三种方式

热门文章

  1. 第二轮冲刺-Runner站立会议08
  2. 使用公用表表达式的递归查询
  3. 01_jeecms建站
  4. 《JS高级程序设计》PART3.对象基础
  5. 怎么样能找到国外的群?
  6. HashMap中数组初始化的秘密
  7. 读书笔记(javascript 高级程序设计)
  8. Windows下C语言连接Oracle数据库
  9. tomcat优化-有改protocol 和 缓存 集群方案
  10. zabbix2.2入门教程之编译安装(一)