代码随想录第十五天

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;
    }
};
相关推荐
计算机安禾18 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio
SatVision炼金士19 小时前
合成孔径雷达干涉测量(InSAR)沉降监测算法体系
算法
wuweijianlove19 小时前
算法稳定性与数值误差传播研究的技术2
算法
计算机安禾19 小时前
【数据结构与算法】第35篇:归并排序与基数排序
c语言·数据结构·vscode·算法·排序算法·哈希算法·visual studio
专注API从业者19 小时前
淘宝商品详情 API 与爬虫技术的边界:合法接入与反爬策略的技术博弈
大数据·数据结构·数据库·爬虫
爱码小白19 小时前
MySQL 单表查询练习题汇总
数据库·python·算法
橘颂TA19 小时前
【笔试】算法的暴力美学——牛客 NC213140 :除2!
c++·算法·结构与算法
汀、人工智能20 小时前
[特殊字符] 第66课:跳跃游戏
数据结构·算法·数据库架构·图论·bfs·跳跃游戏
汀、人工智能20 小时前
[特殊字符] 第70课:加油站
数据结构·算法·数据库架构·图论·bfs·加油站
wsoz20 小时前
Leetcode普通数组-day5、6
c++·算法·leetcode·数组