【LeetCode】100. Same Tree(水)
2016-01-08 09:28
344 查看
Question
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.
Code
/** * 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 return (p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right)); } };
更简洁的Answer:
bool isSameTree(TreeNode* p, TreeNode* q) { if (!p || !q) return q == p; return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); }
相关文章推荐
- js放大镜
- SQL Server中的锁 详解 nolock,rowlock,tablock,xlock,paglock
- UVA 10892 LCM Cardinality(数论 质因数分解)
- iOS开发中使用UILabel设置字体的相关技巧小结
- 数组内存地址
- 常用SQL
- 当时间一天天流逝,一个三十岁的屌丝程序员留下了了什么?
- zookeeper监控之JMX篇
- 内存恶鬼drawRect - 谈画图功能的内存优化
- 解决系统的badgeView
- Unity教程之- UGUI分页效果的实现
- C#中数组、ArrayList和List三者的区别
- Android开发四大组件
- 定义一个job在指定的时间去执行指定的存储过程
- JAVA开发之压缩图片并打成ZIP文件
- 0108 tag
- 分布式架构流程和http请求详解
- Zabbix 安装配置文档Server+Client+Web
- Grunt配置全过程
- 算法直观与对模型的理解(二)