LeetCode872. Leaf-Similar Trees
自己的代码:
# 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的更多相关文章
- 壁虎书6 Decision Trees
Decision Trees are versatile Machine Learning algorithms that can perform both classification and re ...
- sql是如何执行一个查询的!
引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQ ...
- Understanding how SQL Server executes a query
https://www.codeproject.com/Articles/630346/Understanding-how-SQL-Server-executes-a-query https://ww ...
- Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black
A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...
- [Swift]LeetCode872. 叶子相似的树 | Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- Leetcode872.Leaf-Similar Trees叶子相似的树
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两颗二叉树的叶值序列是相同 ...
- Machine Learning Methods: Decision trees and forests
Machine Learning Methods: Decision trees and forests This post contains our crib notes on the basics ...
- Gradient Boosted Regression Trees 2
Gradient Boosted Regression Trees 2 Regularization GBRT provide three knobs to control overfitting ...
- 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 ...
随机推荐
- hello Groovy
Groovy [rocky@www ~]$ curl -s get.sdkman.io 1. 下载 [rocky@www Downloads]$ wget https://dl.bintray.com ...
- js处理包含中文的字符串
场景: js中String类型自带的属性length获取的是字符串的字符数目,但是前端经常会需要限制字符串的显示长度,一个中文字符又大概占两个英文小写字符的显示位置,所以中英文混合的情况下用lengt ...
- less之旅
在遇到less之前,一直和css交往,当less出现在我码农生涯的时候,被她给深深地吸引.从此,less成了自己码农生活里面的一房,css退居二房!那么,less到底有什么魅力让我如此迷上她呢? le ...
- 原生爬虫小Demo
import re from urllib import request class Spider(): url = 'https://www.panda.tv/cate/lol' #[\s\S]匹配 ...
- p2p项目工具类
1.用于存放当前用户的上下文UserContext package com.xmg.p2p.base.util; import javax.servlet.http.HttpSession; impo ...
- fuzz实战之honggfuzz
Honggfuzz实战 前言 本文介绍 libfuzzer 和 afl 联合增强版 honggfuzz .同时介绍利用 honggfuzz 来 fuzz 网络应用服务. 介绍 honggfuzz 也是 ...
- maven打包 springBoot 工程时,默认识别resources目录,习惯使用 resource 目录的需要手动指定静态资源目录
最近项目开发,发现springBoot项目在使用maven打包时,我们静态资源文件都放在resource目录下面,大致如下: 在使用maven打包时,发现静态资源没有打进去.原来springBoot默 ...
- LeetCode题解之Find the Difference
1.题目描述 2.题目分析 比较两个字符串中加入的一个字符,由于可能在字符串中加入一个已经存在的字符,因此使用hash table 去统计字符个数最好. 3.代码 char findTheDiffer ...
- SQLServer 查询使用键查找时锁申请及释放顺序
最近看了高兄的一篇文章,Sql Server 高频,高并发访问中的键查找死锁解析,很有收获,里面讲到了键查找引起的死锁问题. 当然看的过程中,其实自己有个疑问: 对于键查找这类查询,会申请哪些锁,锁申 ...
- Spring MVC 4常用的那些注解
Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解.到目前为止,Spring的版 ...