二叉树的中序遍历

给定一个 二叉树的根节点 root, 返回它的中序遍历。

示例 1:

输入:root = [1,null,2,3]
输出:[1,3,2]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

解题: 

使用递归。

# 我用 pycharm编辑, 会报错 AttributeError: 'list' object has no attribute 'left', 目前还不知道啥原因,知道的小伙伴欢迎给我留言

class Solution(object):
    def inorderTraversal(self, root):
        def inorder(root, res):
            if root is None:
                return
            
            inorder(root.left, res)
            res.append(root.val)
            inorder(root.right, res)
           
        res = []
        inorder(root, res)
        
        return res

 

posted @ 2023-02-13 13:21  JiZhaoG  阅读(15)  评论(0)    收藏  举报