二叉树的广度优先搜索

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None        
    def bfs(self,root):
        if not root:
            return []
        p = [root]
        res = []
        while p:
            q = []
            for i in p:
                res.append(i.val)
                if i.left: q.append(i.left)
                if i.right: q.append(i.right)
            p = q
        return res
    原文作者:小歪与大白兔
    原文地址: https://www.jianshu.com/p/adf4f1e7767c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞