[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 ...
随机推荐
- HTML 学习杂记
代码范例 <?php function testFunc1 () { echo 'testFunc1'; } $b = ; ?> <!DOCTYPE html PUBLIC &quo ...
- appium获取package和activity
输入命令adb shell dumpsys window w |findstr \/ |findstr name=然后在手机端打开app,电脑上按enter就即可显示 以微信为例子 appPackag ...
- jQuery Autocomplete 备忘录
之前使用过此 widget,如今再次需要,发现很多东西已经记不起来了,当然之前用的版本也不一样. 使用之前当然是先认真阅读官方的说明文档和示例,这点很重要,而不是东一块西一点的去网上瞎找资料.Opti ...
- sql随机抽取数据
mysql: select * from tablename order by rand() limit 10 sqlserver: select top 10 * from tablen ...
- c# radiobutton
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- highChart图表
Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用.HighCh ...
- Mysql分析优化查询的方式
一:查询语句分析 1.通过create index idx_colunmsName on tableName(columns)为某个表的某些字段创建索引,注意主键和唯一键都会自动创建索引: 如为表st ...
- Typecho 调用分类文章列表
其中pageSize后面的数字表示调用文章的数量:mid后面的数字表示调用的分类ID; 提示:Typecho分类目录ID的获取方法是把鼠标移到某分类名称上面,在浏览器状态栏显示的mid=后面的数字便是 ...
- 行转列group_concat()函数妙用
测试环境准备: mysql> create table test01(name varchar(20),address varchar(20)); Query OK, 0 rows affect ...
- Windows网络编程(C/C++服务器编程)
Windows服务器网络编程 Linux服务器网络编程