# Leetcode-94. Binary Tree Inorder Traversal 二叉树的中序遍历 （DFS）-python

### 题目

Given a binary tree, return the inorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]

### 思路及代码

#### 1. DFS Recursion

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
def dfs(node):
if not node:
return
dfs(node.left)
ans.append(node.val)
dfs(node.right)
return

ans = []
dfs(root)
return ans


#### 2. Iterative

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


### 复杂度

T = $O(n)$
S = $O(n)$

06-24 26

09-15 58

03-19 4005

07-20 512

01-12 199

10-13 1027

03-05 399

04-16 704

05-22 1万+

01-19 1445

06-23 241

12-26 245

06-01 1930

03-01 19

04-23 1314

08-18 1067

05-25 6442

05-10 4564

05-26 4554

02-28 2157

#### [leetcode] 314. Binary Tree Vertical Order Traversal 解题报告

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客