【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree
题目
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
2.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
Note:
Bonus points if you could solve it both recursively and iteratively.
代码
/*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {if(p==null && q==null) return true;if(p==null || q==null || q.val!=p.val ) return false;if(isSameTree(p.left,q.left)==false || isSameTree(p.right,q.right)==false) return false;return true;}
}
/*** 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;return isEquel(root.left,root.right);}public boolean isEquel(TreeNode root1,TreeNode root2){if(root1==null && root2==null) return true; if(root1==null || root2==null) return false;if(root1.val!=root2.val) return false;return isEquel(root1.left,root2.right) && isEquel(root2.left,root1.right); }
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree相关推荐
- 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目 Given a binary tree, flatten it to a linked list in-place. For example, Given 1/ \2 5/ \ \3 4 6 T ...
- 【LeetCode从零单排】No104 Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- 【LeetCode从零单排】No102 Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【LeetCode从零单排】No129 Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 【LeetCode从零单排】No112 Path Sum
题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
最新文章
- OpenCV对图像进行尺寸变换
- joda time, jackson 与 scala 反射
- MQ怎么保证消息的稳定性和完整性?
- 当随机不够随机:一个在线扑克游戏的教训
- 201107阶段二Linux-qt编程
- 经验证过的跨线程更新辅助类MyInvokeHelper
- 屏蔽武汉ip地址 php,wordpress如何限制屏蔽IP地址
- java utf8 gbk 乱码,java UTF-8转GBK不乱码
- html语言什么时候加引号,什么时候冒号后面不用引号
- Vue项目--仿大麦网移动端
- 国足3比1叙利亚,晋级世预赛12强赛,国足会在12强赛中取得怎样的成绩呢?
- 你是外包,麻烦不要偷吃零食。。。网友:...
- Java 上机----实训操作14----学校录取类
- 【MySql】SQL语句DDL类
- 号外!2021考研政治肖1000大幅修订?你的肖1000Anki牌组笔记已出炉!
- Java系列技术之Spring5框架-钟洪发-专题视频课程
- 8点揭示模具爆裂的具体原因
- oracle中时间差转秒,oracle计算时间秒数差
- LearnOpenGL_study -- 光照贴图
- Selenium 自动化测试(二)部署Selenium Remote Control(Selenium RC)
热门文章
- 2020年高等数学方法与提高(上海理工大学)学习笔记:向量代数与空间解析几何
- 删除win10自带的旧版edge浏览器(亲测有效)
- 台湾国立大学郭彦甫Matlab教程笔记(11) advanced 2D plots 上
- 互动整合营销_初识网络整合营销
- html5毕业作品开场白,毕业典礼主持人开场白
- hocon配置文件_GitHub - Himmelt/Hocon: Simple Hocon Configuration Library [轻量 Hocon 配置库]...
- 后端调用python_【后端开发】python如何调用api接口
- 树莓派4视频输出接口_树莓派第四代来啦!4G内存、支持双屏4K输出和H265硬解
- java 全角_Java全角、半角字符的关系以及转换
- java 论坛_武汉课工场JAVA培训:“真AI、超智能”人工智能大咖论坛解读