leetcode 剑指 Offer 28. 对称的二叉树

2020-10-25  本文已影响0人  Source_Chang

leetcode

C++:

/**
 * 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) {

        if ( !root ) {

            return true;
        }

        return isMiror(root -> left, root -> right);
    }

    bool isMiror(TreeNode* left, TreeNode* right) {

        if ( !left && !right) {

            return true;
        }

        if ( !left || !right ) {

            return false;
        }

        if ( left -> val != right -> val ) {

            return false;
        }

        if (!isMiror(left -> left, right -> right)) {

            return false;
        }

        return isMiror(left -> right, right -> left);
    }
};
上一篇下一篇

猜你喜欢

热点阅读