首页 > 其他 > 详细

LeetCode-Same Treet

时间:2015-01-30 09:04:34      阅读:280      评论:0      收藏:0      [点我收藏+]

<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


LeetCode-Same Treet

原文:http://blog.csdn.net/vanish_dust/article/details/43283959

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!