Help me understand Inorder Traversal without using recursion

后端 未结 14 2697
别跟我提以往
别跟我提以往 2020-12-12 11:44

I am able to understand preorder traversal without using recursion, but I\'m having a hard time with inorder traversal. I just don\'t seem to get it, perhaps, because I have

14条回答
  •  既然无缘
    2020-12-12 12:28

    def print_tree_in(root):
        stack = []
        current = root
        while True:
            while current is not None:
                stack.append(current)
                current = current.getLeft();
            if not stack:
                return
            current = stack.pop()
            print current.getValue()
            while current.getRight is None and stack:
                current = stack.pop()
                print current.getValue()
            current = current.getRight();
    

提交回复
热议问题