# Time:  O(n)
# Space: O(1)
#
# Given a binary tree, return the inorder traversal of its nodes' values.
#
# For example:
# Given binary tree {1,#,2,3},
#   1
#    \
#     2
#    /
#   3
# return [1,3,2].
#
# Note: Recursive solution is trivial, could you do it iteratively?
#

# Definition for a  binary tree node
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# Morris Traversal Solution
class Solution:
    # @param root, a tree node
    # @return a list of integers
    def inorderTraversal(self, root):
        result, prev, cur = [], None, root
        while cur:
            if cur.left is None:
                result.append(cur.val)
                prev = cur
                cur = cur.right
            else:
                node = cur.left
                while node.right and node.right != cur:
                    node = node.right
            
                if node.right is None:
                    node.right = cur
                    cur = cur.left
                else:
                    result.append(cur.val)
                    node.right = None
                    prev = cur
                    cur = cur.right
                
        return result

# Time:  O(n)
# Space: O(n)
# Stack Solution  
class Solution2:
    # @param root, a tree node
    # @return a list of integers
    def inorderTraversal(self, root):
        result, stack, current, last_traversed = [], [], root, None
        while stack or current:
            if current:
                stack.append(current)
                current = current.left
            else:
                parent = stack[-1]
                if parent.right in (None, last_traversed):
                    if parent.right is None:
                        result.append(parent.val)
                    last_traversed = stack.pop()
                else:
                    result.append(parent.val)
                    current = parent.right
        return result

class Solution3:
    # @param root, a tree node
    # @return a list of integers
    def inorderTraversal(self, root):
        result, stack, current = [], [], root
        while stack or current:
            if current:
                stack.append(current)
                current = current.left
            else:
                current = stack.pop()
                result.append(current.val)
                current = current.right
        return result

if __name__ == "__main__":
    root = TreeNode(1)
    root.right = TreeNode(2)
    root.right.left = TreeNode(3)
    result = Solution().inorderTraversal(root)
    print result

@@ -423,16 +423,17 @@
 = None%0A%0A
+%0A
 # Morris
@@ -470,45 +470,101 @@
 tion
-:%0A    # @param root, a tree n
+(object):%0A    def inorderTraversal(self, root):%0A        %22%22%22%0A        :type root: TreeN
 ode%0A    
 # @r
@@ -563,74 +563,41 @@
     
-# @return a list of integers%0A    def inorderTraversal(self, root):
+    :rtype: List%5Bint%5D%0A        %22%22%22
 %0A   
@@ -1264,16 +1264,17 @@
 result%0A%0A
+%0A
 # Time: 
@@ -1290,17 +1290,17 @@
 pace: O(
-n
+h
 )%0A# Stac
@@ -1309,18 +1309,16 @@
 Solution
-  
 %0Aclass S
@@ -1329,178 +1329,95 @@
 ion2
-:%0A    # @param root, a tree node%0A    # @return a list of integers%0A    def inorderTraversal(self, root):%0A        result, stack, current, last_traversed = %5B%5D, %5B%5D, root, Non
+(object):%0A    def inorderTraversal(self, root):%0A        %22%22%22%0A        :type root: TreeNod
 e%0A  
@@ -1422,39 +1422,33 @@
         
-while stack or current:
+:rtype: List%5Bint%5D
 %0A       
@@ -1452,153 +1452,76 @@
     
-    if current:%0A                stack.append(current)%0A                current = current.left%0A            else:%0A                parent =
+%22%22%22%0A        result, stack = %5B%5D, %5B(root, False)%5D%0A        while
  stack
-%5B-1%5D
+:
 %0A   
@@ -1533,63 +1533,39 @@
     
-    if parent.right in (None, last_traversed):%0A        
+root, is_visited = stack.pop()%0A
     
@@ -1575,27 +1575,19 @@
      if 
-parent.righ
+roo
 t is Non
@@ -1609,117 +1609,44 @@
     
-        result.append(parent.val)%0A                    last_traversed = stack.pop()%0A                else:%0A    
+continue%0A            if is_visited:%0A
     
@@ -1671,21 +1671,19 @@
 .append(
-paren
+roo
 t.val)%0A 
@@ -1697,498 +1697,155 @@
     
-        current = parent.right%0A        return result%0A%0Aclass Solution3:%0A    # @param root, a tree node%0A    # @return a list of integers%0A    def inorderTraversal(self, root):%0A        result, stack, current = %5B%5D, %5B%5D, root%0A        while stack or current:%0A            if current:%0A                stack.append(current)%0A                current = current.left%0A            else:%0A                current = stack.pop()%0A                result.append(current.val)%0A                current = current.right
+else:%0A                stack.append((root.right, False))%0A                stack.append((root, True))%0A                stack.append((root.left, False))
 %0A   
@@ -1864,16 +1864,17 @@
 result%0A%0A
+%0A
 if __nam

Update binary-tree-inorder-traversal.py