[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 ...
随机推荐
- centos php5.4 升级 php7
接上篇,edusoho需要php5.5以上版本,于是需要升级本地php php是通过yum默认安装的.以下安装参考 link https://blog.csdn.net/u012569217/arti ...
- Spring Boot设置值:分别用@ConfigurationProperties和@Value给属性设值及其区别
@ConfigurationProperties给属性映射值编写JavaBean/** 将配置文件application.properties中配置的每一个属性值映射到当前类的属性中:* @Confi ...
- FetchType.LAZY和FetchType.EAGER什么区别
1.FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载. 2.FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载. ...
- delphi 条形码
Delphi中打印条码的方法 导读: 1 通过菜单”Component”下的”Import ActiveX”将“Microsoft Access Barcode Control 9.0”控件引入.这个 ...
- android------DDMS files not found: tools\hprof-conv.exe
好久没有Eclipse了,使用一下就遇到坑,使用eclipse突然发生这个问题:DDMS files not found: ***\sdk\tools\hprof-conv.exe,无法连接模拟器 在 ...
- Robot Movement(机器人移动)
中文标题[机器人移动] 这个题目是 Kayak 发布的代码挑战题目. 我认为题目本身描述的不是十分清楚,方法需要返回结果,但是结果没有说明是机器人最后的坐标位置,还是最后的坐标位置距离原点的距离.同时 ...
- S-Nim HDU - 1536
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using ...
- 整体二分求动态区间第k大
比树状数组套主席树不知道高到哪里去了,solve(l,r,L,R)就是对于L,R的操作区间的答案都在l,r区间里,然后递归下去 复杂度O(nlognlogn),每个操作会执行logn次就是o(nlog ...
- MySql(九)索引
一.索引的介绍 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. 二 .索引的作用 约束和加速查找 三.常见的几 ...
- 创建型模式篇(工厂模式Factory Pattern)
一.工厂模式(Factory Pattern) 1.定义: 在软件系统,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.提供一种封 ...