LeetCode_Binary Tree Inorder Traversal

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.
(中序遍历二叉树)

Follow up: Recursive solution is trivial, could you do it iteratively?

Example:



1. 递归

具体实现方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def inorder(self, tree):
if not tree:
return
if tree.left:
self.inorder(tree.left)
self.results.append(tree.val)
if tree.right:
self.inorder(tree.right)

def inorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
self.results = []
self.inorder(root)

return self.results

2. 迭代 & 栈

不断将左子树加入到栈中,然后访问依次访问,最后再访问右子树。具体实现方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []

results, stack = [], []
p = root
while stack or p:
while p:
stack.append(p)
p = p.left
p = stack.pop()
results.append(p.val)
p = p.right

return results