问题链接
https://leetcode.cn/problems/binary-tree-inorder-traversal/description/
解题思路
二叉树的中序遍历。其实深搜和递归是一个道理。搜索必然要通过递归来实现。
中序遍历其实就是:
- 递归遍历左子树
- 操作
- 递归遍历右子树
代码
# 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: Optional[TreeNode]) -> List[int]: res = [] def inorder(root): if not root: return inorder(root.left) res.append(root.val) inorder(root.right) inorder(root) return res
标签:遍历,val,inorder,self,94,right,二叉树,root,中序 From: https://www.cnblogs.com/bjfu-vth/p/17070066.html