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 ...
随机推荐
- jquery之Ajax(一)
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
- CSS流体(自适应)布局下宽度分离原则——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1463 一.简短的前言 ...
- FIO测试磁盘的iops
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- Android 图板之保存图像
(1)为了能适应多种屏幕尺寸的手机,我们在创建图像的时候就要根据用户手机屏幕的宽高像素来创建. (2)该软件将把图形保存到sdcard中,在保存之前,需要检测sdcard是否存在,是否可写入.如通过以 ...
- 【转】教你弄清 OSX 的睡眠模式,以及合法的禁止产生 sleepimage
原文链接 因为之前用的是网上流传的土法来禁止生成 sleepimage,尝到了苦头,而且2次! 大家知道 OSX 有几种睡眠模式,其中 hibernatemode 可以是 0 (传统睡眠方式,不生成 ...
- Oracle 启用归档
[applprod@erp10 ~]$ watch ps -fu applprod[applprod@erp10 ~]$ kill -9 82902 84923 [applprod@erp10 ~]$ ...
- GreenPlum 5.0的安装
基本环境: server IP MDW 172.16.16.31 SDW1 172.16.16.34 SDW2 172.16.16.35 1:检查操作系统是否符合要求,以及系统设置. 我这里使用的系统 ...
- Tomcat 安全设置 及 内存修改
1.删除%tomcatRoot%/webapps目录下的examples.docs文件夹 2.修改%tomcatRoot%/conf/tomcat-users.xml <?xml version ...
- 记录Ubuntu14.04 LTS版本中使用Docker的过程
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...
- CSS学习摘要-布局
注:全文摘自MDN-介绍CSS布局 CSS页面布局技术允许我们拾取网页中的元素,并且控制它们相对正常布局流.周边元素.父容器或者主视口/窗口的位置.在这个模块中将涉及更多关于页面布局技术的细节: 浮动 ...