树02

226. 翻转二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr) return nullptr;
        swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

101. 对称二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public: 
    bool flag = true;
    bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        return isMirror(root->left, root->right);

    }
    bool isMirror(TreeNode* t1, TreeNode* t2) {
        if (!t1 && !t2) return true; // 都是空,对称
        if (!t1 || !t2) return false; // 只有一个空,不对称
        if (t1->val != t2->val) return false; // 值不同,不对称

        // 左的左 和 右的右,左的右 和 右的左 要分别镜像
        return isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
    }
};

104. 二叉树的最大深度

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void countDepth(TreeNode *root, int &result, int &count)
    {
        if(root == nullptr) return;
        count++;
        result = max(result, count);
        if(root->left) 
        {
            countDepth(root->left, result, count);
        }
        if(root->right)
        {
            countDepth(root->right, result, count);
        }
        count--;
    }
    int maxDepth(TreeNode* root) {
        int result = 0, count = 0;
        countDepth(root, result, count);
        return result;

    }
};
int maxDepth(TreeNode* root) {
        return root == nullptr ? 0 : max(maxDepth(root->left), maxDepth(root->right)) + 1;

    }

111. 二叉树的最小深度

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {

        if(root == nullptr) return 0;
        int leftHeight = minDepth(root->left);
        int rightHeight = minDepth(root->right);
        if(root->left && root->right == nullptr)
            return leftHeight + 1;
        if(root->left == nullptr && root->right)
            return rightHeight + 1;
        else
            return min(rightHeight, leftHeight)  + 1;
    }
};
posted @ 2025-07-27 11:32  skyler886  阅读(7)  评论(0)    收藏  举报