LeetCode - Same Tree

来源:互联网 发布:abb机器人编程调试 编辑:程序博客网 时间:2024/05/29 16:30

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.

bool isSameTree(TreeNode *p, TreeNode *q) {    if ((p == NULL) && (q == NULL)) {        return true;    } else if ((p == NULL) || (q == NULL)) {        return false;    } else {    }    if (p->val != q->val) {        return false;    }    return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);}



原创粉丝点击