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