94. Binary Tree Inorder Traversa

2018-04-27  本文已影响0人  GoDeep

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
上一篇下一篇

猜你喜欢

热点阅读