LeetCode - 100. Same Tree

链接

100. Same Tree

题意

判断两棵树是否一样

思路

同时遍历两棵树,判断相对应的结点是否相等即可

代码

C:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    return (p==NULL || q==NULL)? (p==q) :p->val==q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}


posted @ 2017-05-18 12:49  zyoung  阅读(115)  评论(0编辑  收藏  举报