二叉树练习2

226. 翻转二叉树

方法:递归

写法1:前序遍历:中左右

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;
    }
};

写法2:后序遍历:左右中

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

写法3:中序遍历(略有不同):左中左

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

方法: 广度优先遍历–>层序遍历(利用队列)

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode*> q;
        if (root != nullptr) q.push(root);
        while (!q.empty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode* front = q.front();
                q.pop();
                swap(front->left, front->right);
                if (front->left) q.push(front->left);
                if (front->right) q.push(front->right);
            }
        }
        return root;
    }
};

101. 对称二叉树

方法:递归

对称二叉树–>root的左右子树能否互相翻转得到–>左右子树外侧是否对称,内侧是否对称

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

方法:迭代(非层序)

通过队列(也可以用栈)来判断根节点的左子树和右子树的内侧和外侧是否相等

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue<TreeNode*> q;
        if (root == nullptr) return true;
        q.push(root->left);
        q.push(root->right);
        while (!q.empty()) {
            TreeNode* leftNode = q.front();q.pop();
            TreeNode* rightNode = q.front();q.pop();
            if (!leftNode && !rightNode) continue;
            if (!leftNode || !rightNode || (leftNode->val != rightNode->val)) return false;
            q.push(leftNode->left);
            q.push(rightNode->right);
            q.push(leftNode->right);
            q.push(rightNode->left);
        }
        return true;
    }
};

104. 二叉树的最大深度

深度:指从根节点到该节点的最长简单路径边的条数或者节点数(前序)
高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(后序)
根节点的高度就是二叉树的最大深度

方法:递归

通过后序求的根节点高度来求的二叉树最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int leftDepth = maxDepth(root->left);//左
        int rightDepth = maxDepth(root->right);//右
        int depth = 1 + max(leftDepth, rightDepth);//中
        return depth;
    }
};

方法: 广度优先遍历–>层序遍历(利用队列)

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

111. 二叉树的最小深度

这个题不是单纯把上一个的max函数改成min就能过

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。注意是叶子节点。左右孩子都为空的节点才是叶子节点

方法:递归

同上个题也是后序遍历

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        int leftminDepth = minDepth(root->left);//左
        int rightminDepth = minDepth(root->right);//右
        int depth = 1 + min(rightminDepth, leftminDepth);//中
        if (!root->left && root->right) return 1 + rightminDepth;
        if (root->left && !root->right) return 1 + leftminDepth;
        return depth;
    }
};

方法: 广度优先遍历–>层序遍历(利用队列)

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int depth = 0;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()) {
            int size = q.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* front = q.front();
                q.pop();
                if (front->left) q.push(front->left);
                if (front->right) q.push(front->right);
                if (!front->left && !front->right) return depth;
            }
        }
        return depth;
    }
};
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值