Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [1]
Output: [1]
Solution:
"""
前序遍历:打印 - 左 - 右
中序遍历:左 - 打印 - 右
后序遍历:左 - 右 - 打印
"""
class Solution(object):
def inorderTraversal(self, root):
res = []
def dfs(root):
if not root:
return
# 按照 左-打印-右的方式遍历
dfs(root.left)
res.append(root.val)
dfs(root.right)
dfs(root)
return res
- Traversal Inorder Binary Treetraversal inorder binary tree traversal preorder binary tree postorder traversal binary tree traversal binary level order traversal complete 1819c tree binary tree maximum-width-of-binary-tree complete binary 321e tree maximum binary depth tree binary-tree-maximum-path-sum