Java [Leetcode 101]Symmetric Tree
2016-01-13 18:55
357 查看
题目描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
解题思路:
递归法
代码如下:
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 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; return isMirror(root.left, root.right); } public boolean isMirror(TreeNode left, TreeNode right){ if(left == null && right == null) return true; else if(left == null) return false; else if(right == null) return false; else{ if(left.val != right.val) return false; return isMirror(left.right, right.left) && isMirror(left.left, right.right); } } }
相关文章推荐
- 创建你自己的 Java 注解类
- Java笔记3:Eclipse添加jar包
- Java中的函数
- Spring和SpringMVC的区别
- [Java基础]Java基础篇
- springandstruts2整合
- Eclipse调试进入JDK源码
- Java基础之多线程
- Java 继承
- java反射机制之Method invoke执行调用方法例子
- RxJava开发精要8 - 与REST无缝结合-RxJava和Retrofit
- java学习个人笔记---常见的垃圾回收器及存储结构分析
- RxJava开发精要6 - 组合Observables
- RxJava开发精要4 - Observables过滤
- RxJava开发精要5 - Observables变换
- java 批量重命名指定文件夹中所有文件 递增方式命名 前后缀方式命名
- Spring 7大功能模块的作用[转]
- 69道Spring面试题和答案
- windows下搭建MyEclipse开发环境
- Spring与Hibernate整合时的核心类LocalSessionFactoryBean详解