其他分享
首页 > 其他分享> > 101. 对称二叉树

101. 对称二叉树

作者:互联网

✅做题思路or感想:

class Solution {
public:
    bool dfs(TreeNode* left, TreeNode* right) {
        //处理左右节点有空节点的情况,实际上也是处理递归到尽头时的情况,到尽头是就要触底反弹,返回值了
        if (left != nullptr && right == nullptr)return false;
        else if (left == nullptr && right != nullptr)return false;
        else if (left == nullptr && right == nullptr)return true;
        else if (left->val != right->val)return false;
        bool leftTree = dfs(left->left, right->right);	//遍历外侧
        bool rightTree = dfs(left->right, right->left);	//遍历内侧
        return leftTree && rightTree;	//看外侧和内侧是否都对称
    }
    bool isSymmetric(TreeNode* root) {
        if (root == nullptr)return true;
        return dfs(root->left, root->right);
    }
};

标签:right,return,nullptr,dfs,遍历,二叉树,对称,101,left
来源: https://www.cnblogs.com/doomaa/p/16062032.html