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

leetcode1110

时间:2019-07-07 14:30:12      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:tree   end   one   val   roo   elf   sel   nod   return   

 1 class Solution:
 2     def __init__(self):
 3         self.res = []
 4         self.temp = []
 5     
 6     def preNode(self,node,to_delete):
 7         if node == None:
 8             return None
 9         val = node.val
10         if val in to_delete:
11             if node.left != None:
12                 self.temp.append(node.left)
13             if node.right != None:
14                 self.temp.append(node.right)    
15             to_delete.remove(val)
16             return None
17         else:
18             if node.left != None:
19                 leftval = node.left.val
20                 if leftval in to_delete:
21                     t = node.left
22                     if t.left != None:
23                         self.temp.append(t.left)
24                     if t.right != None:
25                         self.temp.append(t.right)
26                     node.left = None
27                     to_delete.remove(leftval)
28                 else:
29                     self.preNode(node.left,to_delete)
30             if node.right != None:
31                 rightval = node.right.val
32                 if rightval in to_delete:
33                     t = node.right
34                     if t.left != None:
35                         self.temp.append(t.left)
36                     if t.right != None:
37                         self.temp.append(t.right)
38                     node.right = None
39                     to_delete.remove(rightval)
40                 else:
41                     self.preNode(node.right,to_delete)
42             return node
43               
44     def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
45         self.temp.append(root)
46         while len(to_delete) > 0 and len(self.temp) > 0:
47             t = self.temp.pop(0)
48             node = self.preNode(t,to_delete)
49             if node != None:
50                 self.res.append(node)
51         self.res += self.temp
52         return self.res

 

leetcode1110

标签:tree   end   one   val   roo   elf   sel   nod   return   

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

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