(C++二叉树02) 翻转二叉树 对称二叉树 二叉树的深度

226、翻转二叉树

递归法:

交换两个结点可以用swap()方法

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == NULL) return NULL;
        TreeNode* tem = root->left;
        root->left = root->right;
        root->right = tem;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

迭代法:

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == NULL) return NULL;
        stack<TreeNode*> sta;
        sta.push(root);
        while(!sta.empty()) {
            TreeNode* tem= sta.top();
            sta.pop();
            swap(tem->left, tem->right);
            if(tem->right) sta.push(tem->right);
            if(tem->left) sta.push(tem->left);
        }
        return root;
    }
};

101、对称二叉树

递归法:

class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right) {
        if(left == NULL && right != NULL) {
            return false;
        }else if(left != NULL && right == NULL) {
            return false;
        }else if(left == NULL && right == NULL){
            return true;
        }else if (left->val != right->val) {
            return false;
        }
        return compare(left->left, right->right) && compare(left->right, right->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(root == NULL) return true;
        return compare(root->left, root->right);
    }
};

104、二叉树的最大深度

递归法:

class Solution {
public:
    int depth(TreeNode* root) {
        if(root == NULL) return 0;
        int left = depth(root->left);
        int right = depth(root->right);
        return left > right ? left + 1 : right + 1;
    }
    int maxDepth(TreeNode* root) {
        if(root == NULL) return 0;
        return depth(root);
    }
};

迭代法:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL) return 0;
        queue<TreeNode*> que;
        int depth = 0;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++;
            for(int i = 0; i < size; i++) {
                TreeNode* tem = que.front();
                que.pop();
                if(tem->left) que.push(tem->left);
                if(tem->right) que.push(tem->right);
            }
        }
        return depth;
    }
};

111、二叉树的最小深度

递归法:

class Solution {
public:
    int depth(TreeNode* root) {
        if(root->left == NULL && root->right == NULL) {
            return 1;
        }else if(root->left != NULL && root->right == NULL) {
            return depth(root->left) + 1;
        }else if(root->left == NULL && root->right != NULL) {
            return depth(root->right) + 1;
        }
        int left = depth(root->left);
        int right = depth(root->right);
        return left > right ? right + 1 : left + 1;
    }
    int minDepth(TreeNode* root) {
        if(root == NULL) return 0;
        return depth(root);
    }
};

迭代法:

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == NULL) return 0;
        queue<TreeNode*> que;
        int depth = 0;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++;
            for(int i = 0; i < size; i++) {
                TreeNode* tem = que.front();
                que.pop();
                if(tem->left == NULL && tem->right == NULL) {
                    return depth;
                }
                if(tem->left) que.push(tem->left);
                if(tem->right) que.push(tem->right);
            }
        }
        return depth;
    }
};

相关推荐

  1. c++

    2024-07-18 11:34:02       58 阅读
  2. c#

    2024-07-18 11:34:02       41 阅读
  3. | 对称问题

    2024-07-18 11:34:02       52 阅读
  4. ---最大深度

    2024-07-18 11:34:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 11:34:02       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 11:34:02       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 11:34:02       62 阅读
  4. Python语言-面向对象

    2024-07-18 11:34:02       72 阅读

热门阅读

  1. LeetCode两数之和

    2024-07-18 11:34:02       20 阅读
  2. postman接囗测试工具详解

    2024-07-18 11:34:02       24 阅读
  3. 三角形与四边形

    2024-07-18 11:34:02       17 阅读
  4. Kylin与BI工具的集成:深入解析与实践

    2024-07-18 11:34:02       27 阅读
  5. 排序之归并排序

    2024-07-18 11:34:02       16 阅读
  6. Servlet 文件上传

    2024-07-18 11:34:02       22 阅读
  7. MQTT 协议的优势

    2024-07-18 11:34:02       20 阅读
  8. oracle 经营范围 设计

    2024-07-18 11:34:02       25 阅读
  9. VDI 和 DaaS 的区别

    2024-07-18 11:34:02       22 阅读
  10. react + pro-components + ts完成单文件上传和批量上传

    2024-07-18 11:34:02       25 阅读