设为首页 加入收藏

TOP

LeetCode:Same Tree
2015-07-24 06:28:29 来源: 作者: 【 】 浏览:43
Tags:LeetCode:Same Tree

Same Tree


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.


解题代码:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) 
    {
        return p ? ( q && p->val == q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right)) : (q == 0) ;
    }
};


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇加快XCode的编译链接速度(200%+.. 下一篇Nubia Z5S 官方4.4 201内测版 内..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: