Leetcode 树 Same Tree
2014-05-11 17:01
369 查看
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Total Accepted: 15922 Total
Submissions: 38418
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.
题意:判断给定的两棵二叉树是不是一样
思路:dfs递归
两棵树一样的条件是
1.它们的根节点一样
2.它们的左子树一样
3.它们的右子树一样
复杂度:时间O(n),空间O(log n)
相关题目:Symmetric Tree
Same Tree
Total Accepted: 15922 TotalSubmissions: 38418
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.
题意:判断给定的两棵二叉树是不是一样
思路:dfs递归
两棵树一样的条件是
1.它们的根节点一样
2.它们的左子树一样
3.它们的右子树一样
复杂度:时间O(n),空间O(log n)
相关题目:Symmetric Tree
/** * 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 && !q) return true; if(!p || !q) return false; return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };
相关文章推荐
- linux下的基本命令
- 命令行执行python模块时提示包找不到的问题
- C26_OC16-OC类型包装
- 小总结
- 不容易系列之(4)——考新郎 杭电 acm 2049 Java
- iftop和iostat安装和简单使用
- 0012-APK-Activity-Wallpaper
- Android文件系统的结构及目录用途、操作方法
- 顺序栈的c语言实现(程序可运行)。。。2014.5.11
- 剪切板使用
- ThreadLocal源码分析
- 推荐一个文献翻译软件--Deja Vu X
- 妈妈的人生哲学:时间会把尊严还给你
- Java异常机制浅析
- 除非指定UpdateCommand,否则数据源SqlDataSource不支持更新操作
- NFC-PN532
- NYOJ105九的余数
- Oracle VM virtualbox共享文件夹创建
- tiny6410裸机实验第5章--------------DDR(代码重定位)
- C25_OC15-OC集合类