LeetCode(25)-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
思路:
- 首先这是一个求关于中心成对称二叉树的题目
- 二叉树的思路就是找到一个递归的突破口
- 首先判断left和right节点的关系来判断,以及(left.left,right.right)以及(left.right,right.left)的关系
代码:
/**
* Definition for a binary tree node.
* 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 isEqual(root.left,root.right);
}
}
public boolean isEqual(TreeNode left,TreeNode right){
if(left == null){
return right == null;
}
if(right == null){
return left == null;
}
if(left.val != right.val){
return false;
}
if(!isEqual(left.left,right.right)){
return false;
}
if(!isEqual(left.right,right.left)){
return false;
}
return true;
}
}
LeetCode(25)-symmetric tree的更多相关文章
- LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...
- LeetCode(101)Symmetric Tree
题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- LeetCode(26)-Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- LeetCode(102) Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- LeetCode(100) Same Tree
题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
随机推荐
- Android加速度传感器
Android加速度传感器 效果图 手机平放桌面的两张截屏,数据一直在刷新 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q487 ...
- 带你深入理解STL之Deque容器
在介绍STL的deque的容器之前,我们先来总结一下vector和list的优缺点.vector在内存中是分配一段连续的内存空间进行存储,其迭代器采用原生指针即可,因此其支持随机访问和存储,支持下标操 ...
- iOS7 CookBook精彩瞬间(三)UIActivityViewController的基本使用及自定义Activity
1.基本使用 UIActivityViewController主要用于分享内容,创建activityView的方法很简单,调用下面的方法创建: [[UIActivityViewController a ...
- java模拟链表
java语言不存在指针,但是我们仍可以用相应的逻辑模拟链表的实现,下面这段代码就是我的一个小伙伴实现的: package com.brucezhang.test; public class ...
- Oracle 总账年终结算流程
1.Oracle 总账应用中年终结算流程包含在开启/关闭期间程序里.当用户开启新一年的第一个期间,开启/关闭期间程序中的"gloire" 流程会完成传送所有收入及支出(损益表 ...
- 自定义android 4.0以上的对话框风格
做个笔记,这里是Dialog的风格,如果是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...
- MVPHelper更新日志 --- 新增常规分包模式
MVPHelper是一款可以自动生成MVP接口以及实现类的android studio插件,彻底解放双手! MVPHelper更新版本啦. 由于之前只支持contract模式,不是很符合大众口味 所以 ...
- Mybatis源码之Statement处理器PreparedStatementHandler(五)
PreparedStatementHandler就是调用PreparedStatement来执行SQL语句,这样在第一次执行sql语句时会进行预编译,在接下来执行相同的SQL语句时会提高数据库性能 源 ...
- Mybatis源码之Statement处理器RoutingStatementHandler(三)
RoutingStatementHandler类似路由器,在其构造函数中会根据Mapper文件中设置的StatementType来选择使用SimpleStatementHandler.Prepared ...
- Oracle分页存储过程及PLSQL中的调用脚本
撰写过程:网上搜集测试了好多的Oracle分页存储过程代码,经整理后终于通过测试,特分享给大家 测试步骤:1.运行创建包命令;2.运行创建存储过程命令;3.运行调用分页存储过程语句 测试环境:wind ...