Monday, November 25, 2013

LeetCode Problem : Same Tree

Problem


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

bool isSameTree(TreeNode *p, TreeNode *q) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    if(!p && !q)
        return true;
    if(!p || !q)
        return false;
    if(p->val != q->val)
        return false;
    return (isSameTree(p->left,q->left) && isSameTree(p->right,q->right));
}

No comments:

Post a Comment