[leetcode.com]算法题目 - Same 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.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (NULL==p && NULL==q) return true; if (NULL!=p && NULL==q) return false; if (NULL !=q && NULL==p) return false; if (p->val == q-> val)
return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
else
return false;
}
};
题目答案
思路:采用递归的方法,先判断(1)p和q都为NULL,则返回true;(2)p和q一个是NULL,一个不是NULL,则返回false;(3)如果(1)和(2)都不是,则证明两个tree都不是NULL,判断p->value是否等于q->value,若不等于,则返回false;若等于,则返回(他们的左子树相等 and 他们的右子树相等)。
[leetcode.com]算法题目 - Same Tree的更多相关文章
- [leetcode.com]算法题目 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 在RedHat 和 Ubuntu 中配置 Delphi 的Linux开发环境(转)
原文地址:http://chapmanworld.com/2016/12/29/configure-delphi-and-redhat-or-ubuntu-for-linux-development/ ...
- node 报错:Uncaught Error: Cannot find module "!!../../../node_modules/extract-webpack-plugin/loader.js
问题出在缺少less和less-loader 因为以上模块使用了less解析. 解决方法在dependencies添加 "less": "^2.7.1", & ...
- thinkphp5 数据库和模型
1.Db和模型的存在只是ThinkPHP5.0架构设计中的职责和定位不同,Db负责的只是数据(表)访问,模型负责的是业务数据和业务逻辑.2.Db和模型最明显的一个区别就是Db查询返回的数据类型为数组( ...
- Java核心技术之类与对象
知识点 1. 一个对象变量并没有实际包含一个对象,而仅仅引用一个对象.new操作符的返回值也是一个引用. 2. 局部变量不会自动地初始化为null,而必须用过调用new或将他们设置为null进行初始化 ...
- android OkHttpUtils 使用JSON数据作为请求参数
如果就直接将JSON作为请求字符串,服务端会出现中文乱码.所以只需要将请求的整个JSON参数字符串编码一次,然后服务端解码一次.我这里服务端使用的servlet,下面会两段代码贴出. android: ...
- Win7 VS2013环境编译Squirrel 3.0.7
Squirrel是一个类似Lua,但是更面向对象的脚本语言. 国内这个介绍很少,环境配置更是没有任何文章提到,花了点时间搞定了,备忘记录下过程. 首先是下载,写本文时Squirrel最新版本为3.0. ...
- Win7 VS2013环境编译Lua5.3.1
主要参考这篇文章,原文有几个错误顺便改正了. 在Windows下使用Visual Studio编译Lua5.3 写本文时Lua官方网站放出的新版本为5.3.1,然后我不知道为啥,神奇的国内不能访问Lu ...
- Win7 VS2013环境编译boost1_58_0
备忘,发现好多不常用的东西不记笔记再想用要重新花时间找,所以试着开始记笔记,写入博客吧. 首先去官网下最新的版本 http://www.boost.org/ 写本文时boost最新版本为1_58_0, ...
- VIM 与 系统剪切版
1, 查看 vim 是否支持 clipboard 功能 $ vim --version | grep clipboard 2, 如果有 +clipboard 则跳过这一步; 如果显示的是 -clipb ...
- PHP源码编译安装
cd php-5.6.0yum -y install libcurl-devel bzip2-devel zlib-devel libjpeg-devel libpng-devel freetype- ...