class Solution:
def __init__(self):
self.prelist = list() def preTree(self,root:TreeNode):
if root != None:
self.preTree(root.left)
self.prelist.append(root.val)
self.preTree(root.right) def Construct(self,ary):
if len(ary)>0:
result = TreeNode(ary[0])
maxnum = ary[0]
maxindex = 0
for i in range(1,len(ary)):
if maxnum < ary[i]:
maxnum = ary[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(ary[:maxindex])
result.right = self.Construct(ary[maxindex+1:])
return result
else:
return None def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
prelist2 = list()
self.preTree(root)
prelist2 = self.prelist[:]
prelist2.append(val)
result = TreeNode(prelist2[0])
maxnum = prelist2[0]
maxindex = 0
for i in range(1,len(prelist2)):
if maxnum < prelist2[i]:
maxnum = prelist2[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(prelist2[:maxindex])
result.right = self.Construct(prelist2[maxindex+1:])
return result

注意第3行,这里是用构造函数来初始化了一个“成员变量”,这样是可以正常工作的。但是如果使用“类变量”,如下面的第2行,在oj上就会报错。

 class Solution:
prelist = list()
def preTree(self,root:TreeNode):
if root != None:
self.preTree(root.left)
self.prelist.append(root.val)
self.preTree(root.right) def Construct(self,ary):
if len(ary)>0:
result = TreeNode(ary[0])
maxnum = ary[0]
maxindex = 0
for i in range(1,len(ary)):
if maxnum < ary[i]:
maxnum = ary[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(ary[:maxindex])
result.right = self.Construct(ary[maxindex+1:])
return result
else:
return None def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
self.preTree(root)
prelist2 = self.prelist[:]
prelist2.append(val)
result = TreeNode(prelist2[0])
maxnum = prelist2[0]
maxindex = 0
for i in range(1,len(prelist2)):
if maxnum < prelist2[i]:
maxnum = prelist2[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(prelist2[:maxindex])
result.right = self.Construct(prelist2[maxindex+1:]) return result

为了解决这个问题,还可以使用一种方式,就是把结果集合传递到前序遍历的方法中,如下面第2行定义的方法,多了一个prelist参数,用于记录结果:

 class Solution:
def preTree(self,root:TreeNode,prelist):
if root != None:
self.preTree(root.left,prelist)
prelist.append(root.val)
self.preTree(root.right,prelist) def Construct(self,ary):
if len(ary)>0:
result = TreeNode(ary[0])
maxnum = ary[0]
maxindex = 0
for i in range(1,len(ary)):
if maxnum < ary[i]:
maxnum = ary[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(ary[:maxindex])
result.right = self.Construct(ary[maxindex+1:])
return result
else:
return None def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
prelist2 = list()
self.preTree(root,prelist2)
prelist2.append(val)
result = TreeNode(prelist2[0])
maxnum = prelist2[0]
maxindex = 0
for i in range(1,len(prelist2)):
if maxnum < prelist2[i]:
maxnum = prelist2[i]
maxindex = i
result.val = maxnum
result.left = self.Construct(prelist2[:maxindex])
result.right = self.Construct(prelist2[maxindex+1:])
return result

总结:使用第1种写法最为稳妥。

leetcode998的更多相关文章

  1. [Swift]LeetCode998. 最大二叉树 II | Maximum Binary Tree II

    We are given the root node of a maximum tree: a tree where every node has a value greater than any o ...

随机推荐

  1. 【nginx】之常用命令

    查看版本号: nginx -s reload  :修改配置后重新加载生效 nginx -s reopen  :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试ngi ...

  2. MIME Types - Complete List

    Suffixes applicable Media type and subtype(s) .3dm x-world/x-3dmf .3dmf x-world/x-3dmf .a applicatio ...

  3. uoj#213. 【UNR #1】争夺圣杯

    http://uoj.ac/problem/209 单调栈求出每个位置x左边第一个大于它的位置L[x]和右第一个不小于它的位置R[x],于是矩形L[x]<=l<=x<=r<=R ...

  4. SQLServer为已有数据的表添加一个自增列

    如果我们从Excel中导入到SQL中大量数据时,如果需要再单独处理特别需要一个字段去表示处理到那条数据了,这时因为没有主键就可以使用以下方式为每条数据添加id ,) not null

  5. 学习笔记之1001 Inventions That Changed the World

    1001 Inventions That Changed the World: Jack Challoner: 9780764161360: Amazon.com: Books https://www ...

  6. eclipse安装、汉化、搭建安卓开发环境

    1.eclipse与jdk的位数(32bit or 64bit )要对应,否则会提示Failed to load JNI shared library.提示这一种错误据说还有另外一种原因就是Path路 ...

  7. ServiceLoader

    先从业务场景分析,要完成数据的分析处理功能.根据数据的不同种类,先调用groovy或者python脚本等中的一种处理数据,处理完数据的后需要流程相同. 要支持处理能力的动态扩展,也就是框架完成后,可以 ...

  8. CentOS安装Git,并上传、下载

    转自:https://blog.csdn.net/u014066037/article/details/70224780 Git 一.安装Git $ yum install git 二. 生成SSH密 ...

  9. 第5章 IP地址和子网划分(3)_子网划分

    6.子网划分 6.1 地址浪费 (1)IPv4公网地址资源日益紧张,为减少浪费,使IP地址能够充分利用,就要用到子网划分技术. (2)传统上一个C类地址,如212.2.3.0/24,其可用的地址范围为 ...

  10. async方法:async+await

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...