Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
題意沒有什麼,就是要你檢查兩棵tree是不是一樣的。
懶得講了 直接貼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 == nullptr || q == nullptr)
{
return (p == q);
}
return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
沒有留言 :
張貼留言