代码随想录第十五天

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;
    }
};
相关推荐
旖-旎14 分钟前
《LeetCode 53 最大子数组和 || LeetCode 918 环形子数组的最大和》
c++·算法·leetcode·动态规划
变量未定义~17 分钟前
单调栈+倍增思想 皇家守卫【算法赛】、单调队列 附近最小
算法
QN1幻化引擎19 分钟前
给 AI 做一次「意识体检」——基于 QN1 幻化引擎的灵鉴意识识别框架与 DalinX V5 实测
大数据·数据结构·人工智能·算法·架构
拂拉氏35 分钟前
【知识讲解】 AVL树从基本成员的介绍到核心接口的实现(插入、判断、删除等等)
数据结构·算法·avl树
可靠的仙人掌1 小时前
SAC(Soft Actor-Critic)算法底座
开发语言·算法·php
海石3 小时前
单调栈复健,顺便,牺牲一下吧,空间复杂度!一切献给AC
算法·leetcode
海石3 小时前
JS击败94%,Hard题想不到动态规划,那就用数组和栈试试
算法·leetcode
码少女3 小时前
数据结构——希尔排序
数据结构·排序算法
星子yu4 小时前
【学习】怎么学好数据结构
数据结构·学习
alphaTao5 小时前
LeetCode 每日一题 2026/7/6-2026/7/12
算法·leetcode