leetcode 100

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

判断两个二叉树是否相同,结点的值和结构都相同。

采用递归来实现。

代码如下:

 /**
* 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;
}
if(p && q && p->val == q->val)
{
if(isSameTree(p->left, q->left))
{
if(isSameTree(p->right, q->right))
{
return true;
}
}
}
return false;
}
};
上一篇:Unity 安卓Jar包的小错误


下一篇:11_Servlet生命周期