leetcode 100. Same Tree

来源:互联网 发布:下载模组的软件 编辑:程序博客网 时间:2024/06/17 12:12

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.



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

accept


0 0
原创粉丝点击