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) {
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(p->val!=q->val) return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
};

same-tree——比较两个二叉树是否相同的更多相关文章

  1. Same Tree 比较两个二叉树是否完全相同

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

  2. same tree(判断两颗二叉树是否相等)

    Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...

  3. 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 ...

  4. LeetCode 617. Merge Two Binary Tree (合并两个二叉树)

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  5. 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】

    迭代版本用的是二叉树的DFS,中的root->right->left +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  6. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  7. leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?

    /* Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...

  8. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  9. 17.Merge Two Binary Trees(合并两个二叉树)

    Level:   Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...

  10. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

随机推荐

  1. 设计模式(一)单例模式:2-懒汉模式(Lazy)

    思想: 相比于饿汉模式,懒汉模式实际中的应用更多,因为在系统中,“被用到时再初始化”是更佳的解决方案. 设计思想与饿汉模式类似,同样是持有一个自身的引用,只是将 new 的动作延迟到 getinsta ...

  2. 优化Angularjs的$watch方法

    Angularjs的$watch相信大家都知道,而且也经常使用,甚至,你还在为它的某些行为感到恼火.比如,一进入页面,它就会调用一次,我明明希望它在我初始化之后,值再次变动才调用.这种行为给我们带来许 ...

  3. 【VBA】标准Sub/Function定义,带ScreenUpdating、On Error GoTo

    [说明] 标准Sub/Function定义,带ScreenUpdating.On Error GoTo Sub AutoFillRole() '--------------- ERROR MSG--- ...

  4. How to use rowspan and colspan in tbody using datatable.js?

    https://stackoverflow.com/questions/27290693/how-to-use-rowspan-and-colspan-in-tbody-using-datatable ...

  5. 思维导图工具 & 原型设计工具

    最近需要画出系统流程架构什么的,就用到了几个思维导图工具 1. XMind -- 思维导图工具 2. MindManager -- 思维导图工具 3. AXURE RP --原型设计工具

  6. 微信小程序底部弹框动画

    在写小程序的时候,一般会碰到底部弹出动画,就像下面这样的效果 直接进入正题 https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-animation.ht ...

  7. GridView 动态绑定控件 OnRowCommand事件触发

    主题:GridView动态生成的控件不能触发OnRowCommand事件,且点击控件按钮后,控件的值会消失. 案例, 由于公司需要绑定的数据列顺序是动态生成的,且有的数据列需要绑定Button控件.所 ...

  8. SPOJ QTREE Query on a tree VI

    You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...

  9. 51 Nod 1678 lyk与gcd

    1678 lyk与gcd 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 这天,lyk又和gcd杠上了.它拥有一个n个数的数列,它想实现两种操作. 1:将  ai  ...

  10. POJ1195Mobile phones

    二维树状数组板子题. #include<cstdio> #include<cstring> #include<iostream> #include<cstdl ...