前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode-Easy 101. Symmetric Tree

Leetcode-Easy 101. Symmetric Tree

作者头像
致Great
发布2018-04-11 16:29:22
6070
发布2018-04-11 16:29:22
举报
文章被收录于专栏:程序生活程序生活

101. Symmetric Tree

  • 描述: 判断一个颗二叉树是否左右对称
  • 思路:

将二叉树的左右节点对放在的队列里,然后出队,判断节点对的左右是否相等。

  • 代码
代码语言:javascript
复制
class Solution:
    def isSymmetric(self, root):
        if not root:
            return True

        dq = collections.deque([(root.left,root.right),])
        while dq:
            node1, node2 = dq.popleft()
            if not node1 and not node2:
                continue
            if not node1 or not node2:
                return False
            if node1.val != node2.val:
                return False
            # node1的左与node2的右相对陈
            dq.append((node1.left,node2.right))
            dq.append((node1.right,node2.left))
        return True
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 101. Symmetric Tree
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档