Leetcode Symmetric Tree Python 学习递归/DFS/BFS

Leetcode 101题 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3
 

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3
 

题目大意是,判断一颗二叉树是否关于根阶段对称。


1. 递归
对于每个节点,判断左右节点的子节点个数是否相等。 同时判断,左节点的左子树和右节点的右子树是否相等,左节点的右子树和右节点的左子树是否相等。 也就是,如果:左的左等于右的右,左的右等于右的左,这棵树就关于根节点对称。

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

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        if not root: return True
        return self.mirror(root.left,root.right)
    
    def mirror(self,left,right):
        if not left or not right: 
            return left == right  #有一点小迷惑
        if left.val != right.val: #切片对比
            return False
        return self.mirror(left.left,right.right) and self.mirror(left.right,right.left)

用搜索算法实现。

DFS(Deep First Search)深度优先搜索。

BFS(Breath First Search)广度优先搜索。

有什么区别与联系? 可以想象深度优先是纵向搜索优先,广度优先是横向搜索优先。 DFS是一条路走到尽头,走到尽头没有结果再返回走另一条路(需要设置地图边界);BFS是周围每次各走一步,没有发现结果,再向四处各走一步。DFS找到结果是真实的走过的路径,而BFS类似于扫描先获取的地图信息,再输出的路径,BFS可以是用空间换时间的策略。

先试试DFS。

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

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        if not root:
            return True
        stackl = [root.left]
        stackr =[root.right]
        while len(stackl) > 0 and len(stackr) > 0:
            left = stackl.pop()
            right = stackr.pop()
            if not left and not right:
                continue
            elif not left or not right:
                return False
            if left.val != right.val:
                return False
            
            stackl.append(left.left)
            stackl.append(left.right)
            stackr.append(right.right)
            stackr.append(right.left)
        return len(stackl) == 0 and len(stackr) == 0

实际上运行超时。Timeout。 但是提交可以通过,很神奇。

再试试BFS。

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

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        if not root:
            return True
        queuel = [root.left]
        queuer = [root.right]
        while len(queuel)>0 and len(queuer)>0:
            left = queuel.pop()
            right = queuer.pop()
            if not left and not right:
                continue
            elif not left or not right:
                return False
            if left.val != right.val:
                return False
            queuel.insert(0,left.left)
            queuel.insert(0,left.right)
            queuer.insert(0,right.right)
            queuer.insert(0,right.left)
        return len(queuel)==0 and len(queuer)==0

区别: stackl.append(xxx) 直接放在栈尾。
queuel.insert(0,xxx)排了序,把xxx放在0号位上。

2020/03/09 加油!
英国,凌晨,雨,明日继续。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值