边工作边刷题:70天一遍leetcode: day 20-1

Binary Tree Inorder Traversal

要点:iteration解法更深入考察了结点指针在binary tree层级间的移动规律。

  • stack用来记录上层未访问的结点,这样之后还可以访问到。而current指针作为一个indicator,如果为null,表示上层结点对应的左子树已经遍历完了,应该pop。如果不为null,应该入栈变为“上层”结点,继续遍历左子树。
  • 当下层左子树遍历完毕,应该访问本结点并且出栈。因为是inorder,出栈并不代表一直向上,而是要继续向右子树开始新的向下。
  • 这题的画面感有一直向左下=>上升=>继续右下
# 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]
        """
        cur = root
        stk = []
        res = []
        while cur or stk:
            if cur:
                stk.append(cur)
                cur=cur.left
            else:
                root = stk.pop()
                res.append(root.val)
                cur=root.right
        return res
        
原文地址:https://www.cnblogs.com/absolute/p/5677937.html