LeetCode872. Leaf-Similar Trees
自己的代码:
# 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相关推荐
- STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树
STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树 最近在做项目时发现一个crash的问题,当时得到的dmp文件显示crash在一个以map为循环变量的循环中,crash位置在如下 ...
- 机器学习论文:《LightGBM: A Highly Efficient Gradient Boosting Decision Tree》
翻译自<LightGBM: A Highly Efficient Gradient Boosting Decision Tree> 摘要 Gradient Boosting Decisio ...
- leetcode310. Minimum Height Trees
题目 For an undirected graph with tree characteristics, we can choose any node as the root. The result ...
- 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 ...
- 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 ...
- 机器学习算法 --- Decision Trees Algorithms
一.Decision Trees Agorithms的简介 决策树算法(Decision Trees Agorithms),是如今最流行的机器学习算法之一,它即能做分类又做回归(不像之前介绍的其他学习 ...
- 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)...
8.1 Trees -->root,children, parent, siblings, leaf; level, degree of element 的基本概念 8.2 Binary Tre ...
- leetcode讲解--872. Leaf-Similar Trees
题目 Consider all the leaves of a binary tree. From left to right order, the values of those leaves fo ...
- leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)
题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...
最新文章
- Hyper-V 网卡 load failed
- angularjs与java_关于angularjs与java结合,获取后台数据并解析的问题
- 【DP】滑雪场的缆车(jzoj 1257)
- mysql select 返回列_SQL / mysql – selectdistinct / UNIQUE,但返回所有列?
- c#反混淆工具de4dot 一般混淆都可以解决
- Python GUI 编程
- 柱状图多维条形图vue_使用D3.js+Vue实现一个简单的柱形图
- 内网通v3.1.2141无捆绑绿色官方版
- 在线摩尔斯密码加密解密工具
- 用Python实现多国语言文章及URL网页在线自动翻译
- 运放的转换速率(压摆率)SR的意义和如何选取
- java程序设计概念对象先行_Java程序设计概念:对象先行(原书第8版)简介,目录书摘...
- Python基础(编码格式、文件、目录)
- 【unity地编】unity制作场景的流程和要点简要
- python个人数据脱敏_数据脱敏的处理方法及查询
- iPad协议接口-稳定版
- 图片站选择什么样的服务器好?
- 运动模糊运动拖影果冻效应分析
- C语言无符号数赋值为负数时的%d%u输出问题解释
- 无线路由器接入局域网的三种方式