leetcood学习笔记-102-二叉树的层次遍历

题目描述:

方法一;

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        ans = []
        stack = [root]
        while stack:
            tem_stack = []
            tem_ans = []
            for i in stack:
                tem_ans.append(i.val)
                if i.left:
                    tem_stack.append(i.left)
                if i.right:
                    tem_stack.append(i.right)
            stack = tem_stack
            ans.append(tem_ans)
        return ans

猜你喜欢

转载自www.cnblogs.com/oldby/p/10574907.html
今日推荐