Tree

汇总树的题目

1
2
3
4
5
6
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

94. 中序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []

class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
res, stack = [], []
while True:
while root:
stack.append(root)
root = root.left
if not stack:
return res
node = stack.pop()
res.append(node.val)
root = node.right

144. 前序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []

class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
res, stack = [], []
if not root:
return res
stack.append(root)
while stack:
node = stack.pop()
res.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return res

145. 后序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root else []

class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
res, stack = [], []
if not root:
return res
stack.append(root)

while stack:
node = stack.pop()
res.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)

return res[::-1]