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
解题思路
递归的方式判断是否对称。
- 判断根节点是否为空;
- 新建一个函数判断二者是否对称,先判断是否有一边为空,其次判断它们是值是否相等,是则进入递归,否则返回错。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def isSymmetric(self, root: TreeNode) -> bool:if root is None:return Trueelse:return self.isSym(root.left, root.right)def isSym(self, left, right):if left is None and right is None:return Trueif left is None or right is None:return Falseif left.val == right.val:tmp1 = self.isSym(left.left, right.right)tmp2 = self.isSym(left.right, right.left)return tmp1 and tmp2else:return False
第二中stack的方式
跟递归差不多的思想,但用stack来实现。
class Solution:def isSymmetric(self, root: TreeNode) -> bool:if root is None:return Truestack = [(root.left, root.right)]while stack:pair = stack.pop(0)left = pair[0]right = pair[1]if left is None and right is None:continueif left is None or right is None:return Falseif left.val==right.val:stack.insert(0, [left.left, right.right])stack.insert(0, [left.right, right.left])else:return Falsereturn True
Leetcode: 101. Symmetric Tree相关推荐
- LeetCode 101. Symmetric Tree
LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...
- leetcode 101 Symmetric Tree
判定两棵树是否严格镜像对称. 解决: BFS bool isSymmetric(TreeNode* root) {if (root == NULL)return true;deque<TreeN ...
- 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /** * Definition for a binary tree no ...
- 101. Symmetric Tree (C语言)
101. Symmetric Tree (C语言) 判断是否为左右镜面对称的二叉树 题目 Given the root of a binary tree, check whether it is a ...
- leetcode python3 简单题101. Symmetric Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...
- LeetCode Algorithm 101. Symmetric Tree
Title 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1/ \2 2/ \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某个结 ...
- 【easy】101. Symmetric Tree
判断一棵二叉树是否对称 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* T ...
最新文章
- nginx rewrite 指令last break区别最详细的解释
- 线段树 区间更新模板
- 盘点66个Pandas函数,轻松搞定“数据清洗”!
- Oracle备份如何到异机还原
- linux线程wait和sleep,java多线程 sleep()和wait()的区别
- 小汤学编程之JAVA基础day11——集合框架:List/Set/Map集合、Collections集合工具类、泛型、TreeMap和TreeSet
- SpringBoot和Mybatis的整合
- Linux简单死锁程序,Linux 死锁例子
- python第三篇:python、flask关系映射
- Vsftp的PASV模式和Port模式及VsFTP配置方案
- socket编程—UDP套接字
- dage手法之 头部和banner ad tpl_header
- android开发实例学习笔记之简易相册的实现
- 开发手机APP的一些心得体会
- 《出版专业基础》2015年版(初级)思考与练习 第六章
- 微信小程序开发一个多少钱
- 毕业后距离就这样慢慢拉开的
- 概述计算机系统的组成和工作原理,计算机系统的组成1.ppt
- win7安装Winpcap4.12显示An error occurred while installing the NPF diver(0x00000430).
- Meth | 新建git项目
热门文章
- RxJavaMVPRetrofit
- javascript加载顺序问题(二)
- ICMP重定向(ICMP Redirect)
- VMware几个版本的比较
- 用python解“用天平找小球”题
- 档案中级职称计算机需要考几个模块,2020年职称申报需要准备哪些档案资料?这些细节必须知道!...
- anaconda中查看python的版本
- 关于python中的self,ins , cls的解释
- 单连接算法与全连接算法
- centos memcached php,centos系统为php安装memcached扩展步骤