代码随想录第十四天

226. 翻转二叉树

先序遍历

复制代码
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;
    }
};

后序遍历

复制代码
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;
    }
};

要注意使用swap的位置,同时使用递归时要注意终止条件和最后的返回值

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 compare(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 out=compare(left->left,right->right);
        bool in=compare(left->right,right->left);

        return out&∈
    }
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr) return true;

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

要注意判断的顺序不能改变否则会引起空指针异常。

必须要先判断,left和right是否为空,如果不为空才可以比较数值,不能先比较数值否则会引起违法操作。

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:
  
    int maxDepth(TreeNode* root) {
        if(root==nullptr) return 0;
        int hl=maxDepth(root->left)+1;
        int hr=maxDepth(root->right)+1;
        return max(hl,hr);
    }
};

要注意递归的顺序,这求二叉树最大深度的题目是后续遍历。

也可以使用层序遍历

复制代码
/**
 * 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 maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=nullptr) que.push(root);//如果根节点不为空,入队
        int h=0;
        while(!que.empty())//当队列为空时结束循环
        {
            h++;
            int size=que.size();

            for(int i=0;i<size;i++)//以每一层的节点个数为循环次数
            {
                TreeNode* node=que.front();
                que.pop();
                if(node->left!=nullptr) que.push(node->left);
                if(node->right!=nullptr) que.push(node->right);

            }
        }
        return h;
    }
};

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 l=minDepth(root->left);
        int r=minDepth(root->right);

        if(root->left==nullptr&&root->right!=nullptr)
        {
            return 1+r;
        }
        if(root->left!=nullptr&&root->right==nullptr)
        {
            return 1+l;
        }

        int res=min(l,r)+1;
        return res;

    }
};

求最小深度采用后续遍历,先确递归函数的参数和返回值,然后确认终止条件

复制代码
if (node == NULL) return 0;

再确认单层递归的逻辑,采用后序遍历,主要确认根的递归逻辑当左子树不为空,右子树为空,返回左子树+1,当右子树不为空,左子树为空,返回右子树+1。最后结果取一个最小值+1.

广度优先遍历-层序遍历

复制代码
/**
 * 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;
        queue<TreeNode*> que;
        que.push(root);
        
        int h=0;
        while(!que.empty())
        {
            h++;
            int size=que.size();
            for(int i=0;i<size;i++)
            {
                TreeNode *node=que.front();
                que.pop();
                if(node->left!=nullptr) que.push(node->left);
                if(node->right!=nullptr) que.push(node->right);

                if(node->left==nullptr&&node->right==nullptr)
                {
                    return h;
                }
                
                    
             
            }
        }
        return h;

    }
};
相关推荐
skywalker_111 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
_日拱一卒1 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode
计算机安禾2 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio
计算机安禾2 小时前
【数据结构与算法】第35篇:归并排序与基数排序
c语言·数据结构·vscode·算法·排序算法·哈希算法·visual studio
专注API从业者2 小时前
淘宝商品详情 API 与爬虫技术的边界:合法接入与反爬策略的技术博弈
大数据·数据结构·数据库·爬虫
汀、人工智能3 小时前
[特殊字符] 第66课:跳跃游戏
数据结构·算法·数据库架构·图论·bfs·跳跃游戏
汀、人工智能3 小时前
[特殊字符] 第70课:加油站
数据结构·算法·数据库架构·图论·bfs·加油站
favour_you___3 小时前
2026_4_8算法练习题
数据结构·c++·算法
汀、人工智能3 小时前
[特殊字符] 第57课:搜索旋转排序数组
数据结构·算法·数据库架构·图论·bfs·搜索旋转排序数组
夏乌_Wx4 小时前
剑指offer | 2.4数据结构相关题目
数据结构·c++·算法·剑指offer·c/c++