博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode-101-Symmetric Tree-二叉树对称问题
阅读量:6938 次
发布时间:2019-06-27

本文共 1309 字,大约阅读时间需要 4 分钟。

topic:

101. Symmetric Tree

Description:

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  3But the following [1,2,2,null,3,null,3] is not:1    / \   2   2    \   \    3    3 Note:Bonus points if you could solve it both recursively and iteratively.

解题思路:1.所谓的对称,是左右相反位置的节点的值判断是否相同。

2.所有的节点对称,是可以从源头追根溯源的。    3.只要出现不同,即可返回即可,否则继续进行处理。

代码如下:

# Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Nonefrom collections import dequeclass Solution:    def isSymmetric(self, root):        """        :type root: TreeNode        :rtype: bool        """        if not root:            return True        nodes_stack=[root.left,root.right]        while nodes_stack:            val_left,val_right=nodes_stack.pop(0),nodes_stack.pop(0)            if not val_left and not val_right:                continue            elif not val_left or not val_right:                return False            elif val_left.val!=val_right.val:                return False            else:                nodes_stack.extend([val_left.left,val_right.right,val_left.right,val_right.left])        return  True

转载地址:http://ausnl.baihongyu.com/

你可能感兴趣的文章
Kali linux 2 使用 Burpsuite 1.6.38
查看>>
二进制 mysql 的安装步骤
查看>>
String类型的属性和方法
查看>>
mysql分区表设计(二)
查看>>
hadoop核心组件zookeeper简介与特点
查看>>
老赵博客
查看>>
最简单的DHCP服务
查看>>
CISCO命令行配置模式
查看>>
phpQuery使用DOMDocument::loadHTML方法产生报错的处理方式
查看>>
linux 命令更换路径之后无法执行
查看>>
YUM源配置
查看>>
C++拷贝构造函数(深拷贝,浅拷贝)
查看>>
我的友情链接
查看>>
shell中变量的间接引用
查看>>
/var/目录下文件详解
查看>>
敏捷活动中的系统思考
查看>>
我的友情链接
查看>>
CentOS6.2+Kerio MailServer开源企业级邮件服务器
查看>>
做个阶段性总结[2012/10/21]
查看>>
Part07 - (图文)NSX系列之检查ESXi主机上VIBs的完整性
查看>>