LeetCode 101. Symmetric Tree

LeetCode 101. Symmetric Tree

Solution1
参考《剑指offer》上的解法:https://blog.csdn.net/allenlzcoder/article/details/79769550

/**
 * 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 isSymmetric(TreeNode* root) {
        return JudgeSym(root, root);
    }

    bool JudgeSym(struct TreeNode *pRoot1, struct TreeNode *pRoot2) {
        if(pRoot1 == NULL && pRoot2 == NULL)
            return true;
        if(pRoot1 == NULL || pRoot2 == NULL)
            return false;
        if(pRoot1->val != pRoot2->val)
            return false;
        return JudgeSym(pRoot1->left, pRoot2->right) && JudgeSym(pRoot1->right, pRoot2->left);
    }
};

猜你喜欢

转载自blog.csdn.net/allenlzcoder/article/details/80742004