Same Tree
2015-12-15 22:10
190 查看
https://leetcode.com/problems/same-tree/
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.
简单的递归,不解释了。
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 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 == 0 && q == 0) { return true; } if(p != 0 && q == 0) { return false; } if(p == 0 && q != 0) { return false; } if(p->val != q->val) { return false; } return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };
简单的递归,不解释了。
相关文章推荐
- 重复描述字符
- gson解析json字符串学习笔记...
- 数据存储---数据持久化
- LeetCode OJ:Merge k Sorted Lists(归并k个链表)
- 第一个React程序HelloWorld
- 设计模式基础——面向对象设计原则(一)
- jstl 的一些随笔
- OC中对@property的见解
- JAVA IO的学习与理解
- Android View触摸事件传递机制
- break、continue和return在程序中的作用
- CentOS7中安装配置PHP7+Nginx
- mysql-结构,命令
- 自己动手写cocos2dx游戏引擎(七)——自动释放池
- 类和结构体的选择
- VS运行时库 /MD、/MDd 和 /MT、/MTd之间的区别
- opengl函数笔记
- OC中new方法与alloc+init及构造方法
- 稀疏表示字典的显示(MATLAB实现代码)
- 比JDK高效的array equals