java 比较两个二叉树是否相等
2016-03-16 11:50
513 查看
题目:
解决算法:
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.
解决算法:
/** * 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 isSameTree(TreeNode p, TreeNode q) { if((p==null&&q!=null)||(p!=null&&q==null)){ return false; } if(p!=null&&q!=null){ if(!(p.val==q.val)){ return false; } } if(p==null&&q==null){ }else{ if(!isSameTree(p.left,q.left)){ return false; } if(!isSameTree(p.right,q.right)){ return false; } } return true; } }
相关文章推荐
- 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位
- Struts2 Convention Plugin ( struts2 零配置 )
- Java关键字final、static使用总结
- Jdk5.0中出现的新特性
- Java数组,去掉重复值、增加、删除数组元素
- Java NIO系列教程(七) FileChannel
- JAVA 中BIO,NIO,AIO的理解
- Spring整合activiti-modeler5.16遇到的小问题
- MyBatis与Spring设置callSettersOnNulls
- Spring整合activiti-modeler5.16遇到的小问题
- Java运算符优先级
- Weblogic操作数据源NameNotFoundException解决办法
- eclipse安装jrebel控件及破解方法,maven项目的热部署
- myeclipse常用配置
- Java finally语句到底是在return之前还是之后执行?
- javascipt继承机制(from阮一峰)
- 使用拦截器实现通用的登陆验证和日志记录
- Java NIO系列教程(六) Selector
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
- maven工程中dubbo与spring整合