leetCode 100. Same Tree 树

100. Same Treenode


Given two binary trees, write a function to check if they are equal or not.ide

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.spa

题目大意:orm

判断两个二叉树是否彻底相同。包括他们节点的内容。递归

代码以下:(递归版)it

/**
 * 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:38io