代码随想录算法训练营DAY15第六章 二叉树part03

目录

[222. 完全二叉树的节点个数](#222. 完全二叉树的节点个数)

[110. 平衡二叉树](#110. 平衡二叉树)

[257. 二叉树的所有路径](#257. 二叉树的所有路径)

[404. 左叶子之和](#404. 左叶子之和)


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

cpp 复制代码
/**
 * 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)return 0;
        return countNodes(root->left)+countNodes(root->right)+1;
    }
};

110. 平衡二叉树

cpp 复制代码
/**
 * 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 {
    int getHeight(TreeNode* node){
        if(!node)return 0;
        int leftHeight=getHeight(node->left);
        if(leftHeight==-1)return -1;
        int rightHeight=getHeight(node->right);
        if(rightHeight==-1)return -1;
        if(abs(leftHeight-rightHeight)>1)return -1;
        else return 1+max(leftHeight,rightHeight);
    }
public:
    bool isBalanced(TreeNode* root) {
        if(getHeight(root)==-1)return false;
        else return true;
    }
};

257. 二叉树的所有路径

cpp 复制代码
/**
 * 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 {
private:
    void dfs(TreeNode* cur, string path, vector<string>& result) {
        path += to_string(cur->val); 
        if (!cur->left&&!cur->right) {
            result.push_back(path);
            return;
        }
        if (cur->left)dfs(cur->left, path + "->", result); 
        if (cur->right)dfs(cur->right, path + "->", result); 
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path;
        if (!root) return result;
        dfs(root, path, result);
        return result;
    }
};

404. 左叶子之和

cpp 复制代码
/**
 * 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) return 0;
        int sum = sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
        TreeNode* left = root->left; 
        if (left && !left->left&& !left->right) sum += left->val; 
        return sum;
    }
};
相关推荐
Sagittarius_A*2 小时前
图像滤波:手撕五大经典滤波(均值 / 高斯 / 中值 / 双边 / 导向)【计算机视觉】
图像处理·python·opencv·算法·计算机视觉·均值算法
seeksky2 小时前
Transformer 注意力机制与序列建模基础
算法
冰暮流星2 小时前
c语言如何实现字符串复制替换
c语言·c++·算法
Swift社区2 小时前
LeetCode 374 猜数字大小 - Swift 题解
算法·leetcode·swift
Coovally AI模型快速验证2 小时前
2026 CES 如何用“视觉”改变生活?机器的“视觉大脑”被点亮
人工智能·深度学习·算法·yolo·生活·无人机
有一个好名字2 小时前
力扣-链表最大孪生和
算法·leetcode·链表
AshinGau2 小时前
Groth16 ZKP: 零知识证明
算法
无限进步_2 小时前
【C语言&数据结构】二叉树链式结构完全指南:从基础到进阶
c语言·开发语言·数据结构·c++·git·算法·visual studio
明月下2 小时前
【视觉算法——Yolo系列】Yolov11下载、训练&推理、量化&转化
算法·yolo