题意:如题

思路:递归解决,同判断对称树的原理差不多。先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的。

 /**
* 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;//都是空
if(!p&&q||p&&!q) return false;//其中1个为空
return (p->val==q->val&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right))?true:false;
}
};

AC代码

LeetCode Same Tree (判断相同树)的更多相关文章

  1. [LeetCode] Same Tree 判断相同树

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

  2. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. [Leetcode] Same tree判断是否为相同树

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

  4. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...

  7. EasyUI Tree判断节点是否是叶

    方法1:  $('#domaincatalog').tree('isLeaf', node.target); 返回true或false ,true表示是叶节点, false即不是 方法2:官方文档中: ...

  8. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  9. 【IT笔试面试题整理】判断一个树是否是另一个的子树

    [试题描述]定义一个函数,输入判断一个树是否是另一个对的子树 You have two very large binary trees: T1, with millions of nodes, and ...

随机推荐

  1. crtmpserver的安装,摄像头视频测试

    下载 svn co --username anonymous --password "" https://svn.rtmpd.com/crtmpserver/branches/1. ...

  2. HashMap加入数据后,会自动根据首字母排序

    1.Map<String, ArrayList<XX>> entityHashMap = new HashMap<>(); 然后增加一些数据,会发现根据String ...

  3. EXPLAIN句法 优化表结构

    EXPLAIN tbl_name or EXPLAIN SELECT select_options EXPLAIN tbl_name是DESC[RIBE] tbl_name或SHOW COLUMNS ...

  4. Cocos-x 3.2:从C++过渡到Lua(转载)

    原文总结的非常好,都是我们学cocos2d-x以来摸索过的东西,如果早有这篇文章就能少走不少弯路了,特此截屏保存.原文链接:http://shahdza.blog.51cto.com/2410787/ ...

  5. 然爸读书笔记(2013-4)----打造facebook

    扎克伯格的真实一面 (1)在公司内部知无不言,扎克伯格在公司内部问答时间.尽可能回答员工的任何问题,保持足够的透明度. (2)员工只有做到对外守口如瓶,我们才能做到对内知无不言. (3)faceboo ...

  6. ExtJS4.2学习(九)属性表格控件PropertyGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-15/178.html ------------- ...

  7. jQuery列表拖动排列-jquery list dragsort插件参数和使用方法

    在编写网页的时候,有时可能需要对ul的li进行排序,今天就给大家推荐使用jquery插件jquery list dragsort实现列表拖动排序效果. 效果如图: jquery list dragso ...

  8. spring中注解事务认识

    1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...

  9. DIY Ruby CPU 分析——Part III

    [编者按]作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是 DIY Ruby CPU Profiling 的第二 ...

  10. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误

    性能分析:出现如下错误: xcode profile  Variable has incomplete type   class “CC_DLL” 解决办法:在 xcode的Build Setting ...