Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
- 1
- / \
- 2 2
- / \ / \
- 3 4 4 3
But the following is not:
- 1
- / \
- 2 2
- \ \
- 3 3
递归解法:
- /**
- * Definition for binary tree
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public boolean isSymmetric(TreeNode root) {
- if(root==null)
- {
- return true;
- }
- else
- {
- return isMirror(root.left,root.right);
- }
- }
- public boolean isMirror(TreeNode left,TreeNode right)
- {
- if(left==null||right==null)//注:不能写成left==null&&right==null,报错
- {
- return left==right;
- }
- else
- {
- return (left.val==right.val)&&isMirror(left.left,right.right)&&isMirror(left.right,right.left);
- }
- }
- }
Symmetric Tree的更多相关文章
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 38. Same Tree && Symmetric Tree
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...
随机推荐
- 使用VS2010创建WebService 发布、测试
http://blog.sina.com.cn/s/blog_45eaa01a0102vopl.html 1 打开VS2010,菜单 文件->新建->项目 2 选择[ASP.net ...
- ubuntu一些基本软件安装方法
ubuntu一些基本软件安装方法 首先说明一下 ubuntu 的软件安装大概有几种方式:1. deb 包的安装方式deb 是 debian 系 Linux 的包管理方式, ubuntu 是属于 deb ...
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...
- CSS隐藏多余文字的几个方法
通常偏移掉字体的方式是 (1)使用text-indent:-9999px; 可是他有一个局限性 他只适用于块级元素block而我们往往有时候想偏移掉的a上的字体所以问题就来了text-indent:- ...
- Docker容器操作中常用命令集合
docker pull 从仓库获取所需要的镜像 docker images 显示本地已有的镜像. docker commit 提交更新后的副本. docker build 创建一个新的镜像 ADD 复 ...
- Unix/Linux进程间通信(二):匿名管道、有名管道 pipe()、mkfifo()
1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...
- C和指针 第十二章 结构体 整体赋值 error: expected expression
定义结构体后整体赋值时发生错误 typedef struct NODE { struct NODE *fwd; struct NODE *bwd; int value; } Node; //声明变量 ...
- java后端制作MD5加密
由于一次业务的需要,我制作了一次密码的修改子业务. 当用户忘记密码的情况下,我需要动态的发给他一个6位的随机密码,通过即时通,短信,微信等.并同时修改数据库中的原密码为这6位的随机密码.让用户再去修改 ...
- CI在ngnix的配置
CI的访问路径是酱紫的: http://localhost/index.php/cname/mname 其中cname为controller的名称,mname为method的名称 但是nginx会把i ...
- espcms联动筛选功能开发
易思后台增加新内容模型,添加字段yewu,fuwu,leixing 修改/interface/article.php (写上新增内容模型的mid——写死的),对这个模型的内容列表写了可以联动筛选的sq ...