代码随想录算法训练营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;
    }
};
相关推荐
筵陌12 分钟前
算法:模拟
算法
Queenie_Charlie40 分钟前
小陶与杠铃片
数据结构·c++·树状数组
We་ct43 分钟前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
renhongxia11 小时前
AI算法实战:逻辑回归在风控场景中的应用
人工智能·深度学习·算法·机器学习·信息可视化·语言模型·逻辑回归
CoderCodingNo1 小时前
【GESP】C++四级/五级练习题 luogu-P1223 排队接水
开发语言·c++·算法
民乐团扒谱机1 小时前
【AI笔记】精密光时频传递技术核心内容总结
人工智能·算法·光学频率梳
云深处@1 小时前
【C++】AVL树
数据结构
CoderCodingNo1 小时前
【GESP】C++五级/四级练习题 luogu-P1413 坚果保龄球
开发语言·c++·算法
Yvonne爱编码2 小时前
JAVA数据结构 DAY4-ArrayList
java·开发语言·数据结构
2301_822366352 小时前
C++中的命令模式变体
开发语言·c++·算法