LeetCode之Same Tree
2014-03-20 14:25
316 查看
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) {
return(
(!p&&!q) ||
(p && q && (p->val==q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right))
);
}
};
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) {
return(
(!p&&!q) ||
(p && q && (p->val==q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right))
);
}
};
相关文章推荐
- 【LeetCode从零单刷】Same Tree
- LeetCode Same Tree OJ 算法 刷题
- LeetCode之Same Tree
- leetcode第49题(same-tree)
- [LeetCode] [Same Tree 2012-09-03 ]
- LeetCode: same-tree
- Leetcode: Same Tree
- leetcode笔记:Same Tree
- LeetCode:Same Tree
- [LeetCode] Same Tree
- LeetCode---Same Tree
- leetcode笔记:Same Tree
- 关于Leetcode中Same Tree一题的理解
- 100. Same Tree Leetcode Python
- LeetCode:Same Tree
- leetcode:100 Same Tree-每日编程第六题
- LeetCode之Same Tree
- leetcode Same Tree
- leetcode Same Tree
- Same Tree(leetcode)