美文网首页
94. Binary Tree Inorder Traversa

94. Binary Tree Inorder Traversa

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-17 08:18 被阅读0次
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def inorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res=[]
            stack=[]
            cur=root
            while stack or cur:
                #add all possible left node into the stack 
                while cur:
                    stack.append(cur)
                    cur=cur.left
                #start adding to result from the left most node 
                cur=stack.pop()
                res.append(cur.val)
                #after reading the root node, start reading the right subtree 
                cur=cur.right
            return res
    
                
    

    相关文章

      网友评论

          本文标题:94. Binary Tree Inorder Traversa

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