LeetCode-Same Treet

时间:2015-01-30 09:07:15   收藏:0   阅读:177

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">题目链接:</span><a target=_blank href="https://oj.leetcode.com/problems/same-tree/" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">https://oj.leetcode.com/problems/same-tree/</a>

题目:

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.

解题思路:比较简单的二叉树,判断两棵树是否相等。算法上面没什么太大的说的,就是递归算法。

class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if (p == NULL && q == NULL)
            return true;
        else if(p == NULL || q == NULL)
            return false;
        bool valSignal = (p->val == q->val) ? true : false;
        bool leftSignal =  isSameTree(p->left,q->left);
        bool rightSignal = isSameTree(p->right,q->right);
        return (valSignal&leftSignal&rightSignal);
    }
};

转载请注明作者:vanish_dust


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!