美文网首页
中序遍历

中序遍历

作者: jojo1313 | 来源:发表于2021-07-05 17:17 被阅读0次

1.结果数组res须参数传递到递归函数inorder,确保递归函数结果写入同一个数组
2.迭代left, 保存root.val, 迭代right

    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

相关文章

网友评论

      本文标题:中序遍历

      本文链接:https://www.haomeiwen.com/subject/tjjiultx.html