Files
interview/16-LeetCode Hot 100/二叉树的中序遍历.md

38 lines
723 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 二叉树的中序遍历 (Binary Tree Inorder Traversal)
## 题目描述
给定一个二叉树的根节点,返回它的中序遍历。
## 解题思路
### 方法一:递归
### 方法二:迭代(栈)
## 解法
```go
func inorderTraversal(root *TreeNode) []int {
result := []int{}
stack := []*TreeNode{}
curr := root
for curr != nil || len(stack) > 0 {
for curr != nil {
stack = append(stack, curr)
curr = curr.Left
}
curr = stack[len(stack)-1]
stack = stack[:len(stack)-1]
result = append(result, curr.Val)
curr = curr.Right
}
return result
}
```
**复杂度:** O(n) 时间O(n) 空间