[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
【思路】
类似于100判断树是否相同,根节点开始分左右两路比较,三种情况讨论。p和q=null、p或q=null、p和q的val相同迭代。
不同在于mirror正好相反, 对left和right比较,即是fun(p1.left,p2.right)&&fun(p1.right,p2.left)。
【代码】
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return fun(root.left,root.right);
}
public boolean fun(TreeNode p1,TreeNode p2) {
if(p1==null&&p2==null)
return true;
if(p1==null||p2==null)
return false;
if(p1.val==p2.val)
return fun(p1.left,p2.right)&&fun(p1.right,p2.left);
return false;
}
}
[Leetcode 101]判断对称树 Symmetric Tree的更多相关文章
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- leetcode-101. 判断对称树 · Tree + 递归
题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 2. 根节点不空,递归判断左右子树.如果左右孩子都空 ...
- [Swift]LeetCode101. 对称二叉树 | Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【Leetcode】【Easy】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- [Leetcode 100]判断二叉树相同 Same Tree
[题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...
- LeetCode Same Tree (判断相同树)
题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
随机推荐
- Within K stops 最短路径 Cheapest Flights Within K Stops
2018-09-19 22:34:28 问题描述: 问题求解: 本题是典型的最短路径的扩展题,可以使用Bellman Ford算法进行求解,需要注意的是在Bellman Ford算法的时候需要额外申请 ...
- Mongodb 分享(一)
Mongodb使用基础知识: 一.简介 1.mongodb是什么? 1)MongoDB 是一个基于分布式文件存储的数据库.由 )mongodb 客户端:NoSQL Manager for MongoD ...
- linux常用命令及系统常见符号
常用命令 1.start x 进入界面 2.shutdown -h now 立刻关机 shutdown -r now 立刻重新启动 reboot 立刻重新启动 3.su root 切换成超级管理员 4 ...
- Ubuntu16.04安装
这篇博文主要是想记录自己以前安装ubuntu的经历.当然参考了很多其他优秀的文章,在这里推荐一篇博客,请踩这个地址-->http://www.cnblogs.com/Duane/p/542421 ...
- 20190102xlVBA_多表按姓名同时拆分
Sub 多表按姓名同时拆分20190102() AppSettings Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA ...
- Linux简介和安装
Andrew S. Tanenbaum参考Unix,写了Minix,并开源,Linus Torvalds以其为模板写了Linux. Linux包含内核版本和发行版本. Linux内核版本 Linux内 ...
- wdcp环境安装filephp扩展
网址 :https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80838424
- Vrrp和Hsrp的区别
VRRP原理协议简述简单来说,VRRP是一种容错协议,它为具有组播或广播能力的局域网(如以太网)设计,它保证当局域网内主机的下一跳路由器出现故障时,可以及时的由另一台路由器来代替,从而保持通讯的连续性 ...
- python基础之生成器,生成器函数,列表推导式
内容梗概: 1. 生成器和生成器函数. 2. 列表推导式. 1.生成器函数1.1 生成器函数. 就是把return换成yield def gen(): print("爽歪歪") y ...
- Dynamic Shortest Path CodeForces - 843D (动态最短路)
大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...