代码随想录第十五天

222. 完全二叉树的节点个数

方法一采用广度优先遍历-层序遍历

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

方法二:普通二叉树------递归遍历

复制代码
/**
 * 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 countNodes(TreeNode* root) {
        if(root==nullptr) return 0;
        int left=countNodes(root->left);
        int right=countNodes(root->right);

        int res=left+right+1;
        return res;
    }
};

方法三完全二叉树-递归

复制代码
/**
 * 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 countNodes(TreeNode* root) {
        if(root==nullptr) return 0;
        TreeNode* left=root->left;
        TreeNode* right=root->right;
        int lh=0,rh=0;
        while(left)
        {
            left=left->left;
            lh++;
        }
        while(right)
        {
            right=right->right;
            rh++;
        }
        if(lh==rh)
        {
            return (2<<lh)-1;
        }//递归终止判断条件


        return countNodes(root->left)+countNodes(root->right)+1;
    }
};

110. 平衡二叉树

复制代码
/**
 * 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 function(TreeNode* root)
    {
        if(root==nullptr) return 0;
        int lefth=function(root->left);
        int righth=function(root->right);
        int res=max(lefth,righth)+1;

        return res;
    }
    bool isBalanced(TreeNode* root) {
        if(root==nullptr) return true;
        int h1=function(root->left);
        int h2=function(root->right);
        if(abs(h1-h2)>1)
        {
            return false;
        }//递归终止条件
        bool a=isBalanced(root->left);
        bool b=isBalanced(root->right);

        return a&&b;
        
    }
};

先写出判断左右子树高度的函数,在判断平衡二叉树里面写出终止判断条件,当左右子树高度相差一的时候返回,再进行递归遍历

复制代码
/**
 * 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 function(TreeNode* root)
    {
        if(root==nullptr) return 0;//递归终止逻辑

        //单层递归逻辑
        int lefth=function(root->left);
        if(lefth==-1) return -1;//如果子树已经不平衡的话,就直接返回
        int righth=function(root->right);
        if(righth==-1) return -1;

        int res=0;
        if(abs(lefth-righth)>1)
        {
            res=-1;
        }
        else
        {
            res=max(lefth,righth)+1;
        }
        return res;
    }
    bool isBalanced(TreeNode* root) {
        if(root==nullptr) return true;
        int h=function(root);
        if(h==-1)
        {
            return false;
        }
        return true;
    }
};

257. 二叉树的所有路径

因为你可能在想:

"当我走到一个节点,发现它是空的,说明上一个节点是最后一个有效节点,所以那条路径结束了。"

这个想法有道理,但不正确的原因

  1. 时机不对 :在空节点处,我们已经离开了真正的叶子节点

  2. 重复问题:一个叶子节点有两个空孩子,会被记录两次

  3. 语义不清:空节点不是树的组成部分

所以我们进行判断时必须是判断叶子节点,不能使用判断空节点

复制代码
/**
 * 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 traverse(TreeNode *cur,vector<int> &path,vector<string> & res)
    {
        path.push_back(cur->val);

        if(cur->left==nullptr&&cur->right==nullptr)//判断是否为路径终点,如果为终点进行转换
        {
            string spath;
            for(int i=0;i<path.size()-1;i++)
            {
                spath+=to_string(path[i]);
                spath+="->";
            }
            spath+=to_string(path[path.size()-1]);
            res.push_back(spath);
            return;
        }

        if(cur->left)
        {
            traverse(cur->left,path,res);
            path.pop_back();
        }
        if(cur->right)
        {
            traverse(cur->right,path,res);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<int> path;
        vector<string> res;
        traverse(root,path,res);
        return res;
    }
};

vector容器后插和删除要使用push_back和pop_back

404. 左叶子之和

迭代法-层序遍历-广度优先遍历

复制代码
/**
 * 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 sumOfLeftLeaves(TreeNode* root) {
        if(root==nullptr) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int res=0;
        while(!que.empty())
        {
            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->left->left==nullptr&&node->left->right==nullptr)
                {
                    res=res+node->left->val;
                }
            }
        }
        return res;
    }
};

递归法

复制代码
/**
 * 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 sumOfLeftLeaves(TreeNode* root) {
        if(root==nullptr) return 0;
        if(root->left==nullptr&&root->right==nullptr) return 0;

        int leftnum=sumOfLeftLeaves(root->left);
        if(root->left!=nullptr&&root->left->left==nullptr&&root->left->right==nullptr)
        {
            leftnum=root->left->val;
        }
        int rightnum=sumOfLeftLeaves(root->right);

        int sum=leftnum+rightnum;
        return sum;
    }
};
相关推荐
Wect4 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP16 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub1 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP2 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP2 天前
一文搞懂激活函数!
算法·面试