新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

leetCode100.SameTree树

100. Same Tree

创新互联公司主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、网络营销推广、网站程序开发、HTML5响应式成都网站建设手机网站开发、微商城、网站托管及成都网站维护公司、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都自拌料搅拌车行业客户提供了网站设计服务。

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 a binary tree node.
 * 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) {
        bool childResult;
        if( NULL == p && NULL == q)
            return true;
        if( NULL != p && NULL != q && p->val == q->val)
        {
            return childResult = isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
        }
        return false;
        
    }
};

2016-08-07 00:01:38


文章标题:leetCode100.SameTree树
文章地址:http://www.wjwzjz.com/article/iecgci.html
在线咨询
服务热线
服务热线:028-86922220
TOP