[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 ...
随机推荐
- python float转为decimal
73.2413793103 ======= 73.2414 <type 'float'> ======= <class 'decimal.Decimal'> 当断言这两个值相等 ...
- [Solution] The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
HttpServlet需要tomcat等. 右键project点开properties>project facets> 在右侧栏的Runtime tab中勾选tomcat或者新建tomca ...
- centos7:mysql-5.7.23安装(二进制安装)
mysql有二进制码安装,和源码编译安装(mysql5.5使用cmake安装,mysql5.7需要安装boost依赖安装),因为boost依赖安装麻烦,所以用二进制码安装 MySql 5.7.23安装 ...
- ultragrid
foreach (UltraGridColumn aCol in this.ultraGrid1.DisplayLayout.Bands[0].Columns){this.ultraGrid1.Dis ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 添加分页组件
jQuery EasyUI 数据网格 - 添加分页组件 本实例演示如何从服务器端加载数据,如何添加分页组件(pagination)到数据网格(datagrid). 创建数据网格(DataGrid) 为 ...
- 【消息队列】kafka是如何保证消息不被重复消费的
一.kafka自带的消费机制 kafka有个offset的概念,当每个消息被写进去后,都有一个offset,代表他的序号,然后consumer消费该数据之后,隔一段时间,会把自己消费过的消息的offs ...
- Confluence 6 设置你的个人空间主页
不论你是否正在使用个人空间为沙盒来测试一些内容,组合灯显示是如何工作的,一个能够导航到其他空间和内容的页面,或者一些完全不同的东西.下面一些红能够帮助你在你的个人空间中更加有效的使用和发布信息. 使用 ...
- docker 基本操作
# 常用命令 docker run 镜像 docker images 查看所有镜像 docke ps 查看运行中的容器 docker ps -a 列出所有容器 docker st ...
- bzoj4804: 欧拉心算 欧拉筛
题意:求\(\sum_{i=1}^n\sum_{j=1}^n\phi(gcd(i,j))\) 题解:\(\sum_{i==1}^n\sum_{j=1}^n\sum_{d=1}^n[gcd(i,j)== ...
- php curl 并发
1.比较普通的curl 请求 $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 curl_seto ...