Skip to content

Commit ac06b23

Browse files
committed
2020-01-07
1 parent c7a5eb7 commit ac06b23

File tree

8 files changed

+44
-87
lines changed

8 files changed

+44
-87
lines changed

0094.二叉树的中序遍历/0094-二叉树的中序遍历.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ def inorderTraversal(self, root):
1111
:type root: TreeNode
1212
:rtype: List[int]
1313
"""
14-
result = list()
15-
self.generate(root, result)
16-
return result
14+
stack = []
15+
cur = root
16+
res = []
17+
while cur or stack:
18+
if cur:
19+
stack.append(cur)
20+
cur = cur.left
21+
else:
22+
cur = stack.pop()
23+
res.append(cur.val)
24+
cur = cur.right
25+
return res
1726

18-
19-
20-
def generate(self, root, result):
21-
22-
if not root:
23-
return
24-
25-
if root.left:
26-
self.generate(root.left, result)
27-
28-
result.append(root.val)
29-
30-
if root.right:
31-
self.generate(root.right, result)

0144.二叉树的前序遍历/0144-二叉树的前序遍历.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ def preorderTraversal(self, root):
1111
:type root: TreeNode
1212
:rtype: List[int]
1313
"""
14-
if not root:
15-
return []
1614
stack = [root]
1715
res = []
1816
while stack:
1917
cur = stack.pop()
20-
res.append(cur.val)
21-
22-
if cur.right:
18+
if cur:
19+
res.append(cur.val)
2320
stack.append(cur.right)
24-
if cur.left:
2521
stack.append(cur.left)
26-
return res
27-
22+
return res

0145.二叉树的后序遍历/0145-二叉树的后序遍历.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ def postorderTraversal(self, root):
1111
:type root: TreeNode
1212
:rtype: List[int]
1313
"""
14-
if not root:
15-
return []
16-
1714
stack = [root]
1815
res = []
1916
while stack:
2017
cur = stack.pop()
21-
res.append(cur.val)
22-
if cur.left:
18+
if cur:
19+
res.append(cur.val)
2320
stack.append(cur.left)
24-
if cur.right:
2521
stack.append(cur.right)
26-
2722
return res[::-1]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
# Definition for a Node.
33
class Node(object):
4-
def __init__(self, val, children):
4+
def __init__(self, val=None, children=None):
55
self.val = val
66
self.children = children
77
"""
@@ -12,11 +12,9 @@ def preorder(self, root):
1212
:rtype: List[int]
1313
"""
1414
if not root:
15-
return list()
16-
result = list()
17-
result.append(root.val)
18-
19-
for leaf in root.children:
20-
result += self.preorder(leaf)
21-
22-
return result
15+
return []
16+
17+
res = [root.val]
18+
for child in root.children:
19+
res += self.preorder(child)
20+
return res
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
# Definition for a Node.
33
class Node(object):
4-
def __init__(self, val, children):
4+
def __init__(self, val=None, children=None):
55
self.val = val
66
self.children = children
77
"""
@@ -13,8 +13,7 @@ def postorder(self, root):
1313
"""
1414
if not root:
1515
return []
16-
res = list()
17-
for leaf in root.children:
18-
res += self.postorder(leaf)
19-
16+
res = []
17+
for child in root.children:
18+
res += self.postorder(child)
2019
return res + [root.val]

0700.二叉搜索树中的搜索/0700-二叉搜索树中的搜索.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ def searchBST(self, root, val):
1212
:type val: int
1313
:rtype: TreeNode
1414
"""
15-
# print root.val
16-
if not root:
17-
return None
18-
if root.val == val:
15+
if not root or root.val == val:
1916
return root
20-
elif root.val > val:
17+
18+
if root.val > val:
2119
return self.searchBST(root.left, val)
22-
else:
23-
return self.searchBST(root.right, val)
24-
25-
20+
elif root.val < val:
21+
return self.searchBST(root.right, val)

0814.二叉树剪枝/0814-二叉树剪枝.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ def pruneTree(self, root):
1212
:rtype: TreeNode
1313
"""
1414
if not root:
15-
return None
16-
# print root.val, self.generate(root.left), self.generate(root.right)
15+
return None
1716
root.left = self.pruneTree(root.left)
1817
root.right = self.pruneTree(root.right)
19-
if root.left == None and root.right == None and (root.val == 0):
20-
return None
21-
return root
22-
23-
18+
if not root.left and not root.right and not root.val:
19+
root = None
20+
return root

1008.先序遍历构造二叉树/1008-先序遍历构造二叉树.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,12 @@ def bstFromPreorder(self, preorder):
1111
:type preorder: List[int]
1212
:rtype: TreeNode
1313
"""
14-
inorder = sorted(preorder)
15-
16-
return self.buildTree(preorder, inorder)
17-
18-
19-
20-
def buildTree(self, preorder, inorder): # leetcode 105
21-
"""
22-
:type preorder: List[int]
23-
:type inorder: List[int]
24-
:rtype: TreeNode
25-
"""
2614
if not preorder:
27-
return None
28-
29-
root = TreeNode(preorder[0])
30-
left_inorder = inorder[: inorder.index(root.val)]
31-
right_inorder = inorder[inorder.index(root.val) + 1 :]
32-
33-
l_left = len(left_inorder)
34-
left_preorder = preorder[1:l_left + 1]
35-
right_preorder = preorder[l_left + 1 :]
36-
37-
root.left = self.buildTree(left_preorder, left_inorder)
38-
root.right = self.buildTree(right_preorder, right_inorder)
39-
15+
return None
16+
inorder = sorted(preorder)
17+
18+
idx = inorder.index(preorder[0])
19+
root = TreeNode(preorder[0])
20+
root.left = self.bstFromPreorder(preorder[1:idx + 1])
21+
root.right = self.bstFromPreorder(preorder[idx + 1:])
4022
return root

0 commit comments

Comments
 (0)