就是判断两棵树的值和结构是否相同

注意:要判断是否所有的树节点是否为NULL

 /**
* Definition for a binary tree node.
* 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) {
if(!p && !q) {
return true;
}
else if(!p||!q){
return false;
}
else{
if(p->val != q->val) return false;
else return isSameTree(p->left, q->left) &&
isSameTree(p->right, q->right);
}
}
};

Leetcode 100 Same Tree 二叉树的更多相关文章

  1. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  2. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  3. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  4. (二叉树 递归 DFS) leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  5. [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

    描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...

  6. LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. LeetCode 100. Same Tree (相同的树)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  9. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. AOP技术应用和研究--OOP

    1,软件编程技术的发展 软件编程技术与程序设计语言是分不开的.过去的几十年中,程序设计语言对抽象机制的支持程度不断提高:从机器语言到汇编语言,到高级语言,再到面向对象语言.每一种新的程序设计语言的出现 ...

  2. 得到INI文件所有Section(所有节点名称)

    char SectionNames[MAX_PATH],*pSectionName; ZeroMemory(SectionNames,MAX_PATH); GetPrivateProfileSecti ...

  3. 【Codeforces Round #439 (Div. 2) C】The Intriguing Obsession

    [链接] 链接 [题意] 给你3种颜色的点. 每种颜色分别a,b,c个. 现在让你在这些点之间加边. 使得,同种颜色的点之间,要么不连通,要么连通,且最短路至少为3 边是无向边. 让你输出方案数 [题 ...

  4. [AngularFire2] Update multi collections at the same time with FirebaseRef

    At some point, you might need to udpate multi collections and those collections should all updated s ...

  5. 用css3解决移动端页面自适应横屏竖屏的思考

    之前对于横屏的webapp做过一些尝试,可是始终不是非常好的解决方式,前段时间又接触了类似的需求,尝试了感觉更好的解决方式. 之前的方法写的博客:移动网页横竖屏兼容适应的一些体会 这里举的样例还是平时 ...

  6. null与对象的复杂关系(typeof null的结果是object的原因)

    原文 简书原文:https://www.jianshu.com/p/c1608452d056 前言 对象是 JavaScript 的基础.在 JavaScript 中一共有六种主要类型(术语是“语言类 ...

  7. POJ 3627 Bookshelf 贪心 水~

    最近学业上堕落成渣了.得开始好好学习了. 还有呀,相家了,好久没回去啦~ 还有和那谁谁谁... 嗯,不能发表悲观言论.说好的. 如果这么点坎坷都过不去的话,那么这情感也太脆弱. ----------- ...

  8. Intent七大属性之总结 分类: H1_ANDROID 2013-11-10 09:41 1074人阅读 评论(0) 收藏

    参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...

  9. Call to a member function assign() on a non-object;thinkphp中报错

    这个在自己写的类中 需要function __construct(){parent::__construct();}继承父类构造函数 当发生这个错误的时候,需要在构造函数中集成父类构造

  10. 洛谷 P2197 nim游戏

    洛谷 P2197 nim游戏 题目描述 甲,乙两个人玩Nim取石子游戏. nim游戏的规则是这样的:地上有n堆石子(每堆石子数量小于10000),每人每次可从任意一堆石子里取出任意多枚石子扔掉,可以取 ...