94. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
1

2
/
3

Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?

还是维护一个p指针吧,不要直接用stack里面的值

class Solution:
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root: return []
        res = []
        st = []
        p = root
        while p or st:
            while p:
                st.append(p)
                p = p.left
            t = st.pop()
            res.append(t.val)
            p = t.right
        return res
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容