100. Same Tree
2015-12-30 21:51
253 查看
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.
isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)读者可以想想这个
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) { return true; } if(p == null || q == null) { return false; } if ((p.val == q.val) && (isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right))) { return true; }else { return false; } } }其实后面的if判断还可以升级下,使用递归判断return
isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)读者可以想想这个
相关文章推荐
- Linux别名与常用快捷键
- [OC] UITabBarController
- C++11实现一个自动注册的工厂
- RXAndroid Single的使用
- Hackerearth.com编程问题解题思路系列:Roy's Chocolates
- 安卓apk下载安装
- SystemUI下拉通知栏的源码分析
- CDOJ 1269 ZhangYu Speech 数组处理
- stdlib.h
- javaWeb之Servlet的工作原理与生命周期
- ftrace
- LNMP 1.6 常见的502问题解决
- Linux入门导读
- 模拟退火算法
- bzoj4008: [HNOI2015]亚瑟王【期望dp】
- 几种元素的用法
- 实习电面小记
- byte[]转为string,再用substring取值,在eclipse和exe里取得值不同
- Collections.Sort()方法的使用方式
- windows下配置eclipse + Python + PyDev + tornado,并使用pylint进行静态检测