代码随想录第十五天

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;
    }
};
相关推荐
XX風6 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT067 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠7 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
想进个大厂7 小时前
代码随想录day37动态规划part05
算法
sali-tec7 小时前
C# 基于OpenCv的视觉工作流-章22-Harris角点
图像处理·人工智能·opencv·算法·计算机视觉
子春一7 小时前
Flutter for OpenHarmony:构建一个 Flutter 四色猜谜游戏,深入解析密码逻辑、反馈算法与经典益智游戏重构
算法·flutter·游戏
人道领域8 小时前
AI抢人大战:谁在收割你的红包
大数据·人工智能·算法
TracyCoder1238 小时前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode
A尘埃8 小时前
电信运营商用户分群与精准运营(K-Means聚类)
算法·kmeans·聚类