码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode998

时间:2019-03-04 17:32:19      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:app   前序遍历   tom   node   str   工作   tree   pre   leetcode   

 1 class Solution:
 2     def __init__(self):
 3         self.prelist = list()
 4 
 5     def preTree(self,root:TreeNode):
 6         if root != None:            
 7             self.preTree(root.left)
 8             self.prelist.append(root.val)
 9             self.preTree(root.right)
10 
11     def Construct(self,ary):
12         if len(ary)>0:
13             result = TreeNode(ary[0])
14             maxnum = ary[0]
15             maxindex = 0
16             for i in range(1,len(ary)):
17                 if maxnum < ary[i]:
18                     maxnum = ary[i]
19                     maxindex = i
20             result.val = maxnum
21             result.left = self.Construct(ary[:maxindex])
22             result.right = self.Construct(ary[maxindex+1:])
23             return result
24         else:
25             return None
26 
27 
28 
29     def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
30         prelist2 = list()
31         self.preTree(root)
32         prelist2 = self.prelist[:]
33         prelist2.append(val)
34         result = TreeNode(prelist2[0])
35         maxnum = prelist2[0]
36         maxindex = 0
37         for i in range(1,len(prelist2)):
38             if maxnum < prelist2[i]:
39                 maxnum = prelist2[i]
40                 maxindex = i
41         result.val = maxnum
42         result.left = self.Construct(prelist2[:maxindex])
43         result.right = self.Construct(prelist2[maxindex+1:])
44         return result

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

 1 class Solution:
 2     prelist = list()
 3     def preTree(self,root:TreeNode):
 4         if root != None:            
 5             self.preTree(root.left)
 6             self.prelist.append(root.val)
 7             self.preTree(root.right)
 8 
 9     def Construct(self,ary):
10         if len(ary)>0:
11             result = TreeNode(ary[0])
12             maxnum = ary[0]
13             maxindex = 0
14             for i in range(1,len(ary)):
15                 if maxnum < ary[i]:
16                     maxnum = ary[i]
17                     maxindex = i
18             result.val = maxnum
19             result.left = self.Construct(ary[:maxindex])
20             result.right = self.Construct(ary[maxindex+1:])
21             return result
22         else:
23             return None
24 
25 
26 
27     def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
28         self.preTree(root)
29         prelist2 = self.prelist[:]
30         prelist2.append(val)
31         result = TreeNode(prelist2[0])
32         maxnum = prelist2[0]
33         maxindex = 0
34         for i in range(1,len(prelist2)):
35             if maxnum < prelist2[i]:
36                 maxnum = prelist2[i]
37                 maxindex = i
38         result.val = maxnum
39         result.left = self.Construct(prelist2[:maxindex])
40         result.right = self.Construct(prelist2[maxindex+1:])
41 
42         return result

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

 1 class Solution:
 2     def preTree(self,root:TreeNode,prelist):
 3         if root != None:            
 4             self.preTree(root.left,prelist)
 5             prelist.append(root.val)
 6             self.preTree(root.right,prelist)
 7 
 8     def Construct(self,ary):
 9         if len(ary)>0:
10             result = TreeNode(ary[0])
11             maxnum = ary[0]
12             maxindex = 0
13             for i in range(1,len(ary)):
14                 if maxnum < ary[i]:
15                     maxnum = ary[i]
16                     maxindex = i
17             result.val = maxnum
18             result.left = self.Construct(ary[:maxindex])
19             result.right = self.Construct(ary[maxindex+1:])
20             return result
21         else:
22             return None
23 
24 
25 
26     def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
27         prelist2 = list()
28         self.preTree(root,prelist2)
29         prelist2.append(val)
30         result = TreeNode(prelist2[0])
31         maxnum = prelist2[0]
32         maxindex = 0
33         for i in range(1,len(prelist2)):
34             if maxnum < prelist2[i]:
35                 maxnum = prelist2[i]
36                 maxindex = i
37         result.val = maxnum
38         result.left = self.Construct(prelist2[:maxindex])
39         result.right = self.Construct(prelist2[maxindex+1:])
40         return result

 

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

leetcode998

标签:app   前序遍历   tom   node   str   工作   tree   pre   leetcode   

原文地址:https://www.cnblogs.com/asenyang/p/10471677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!