代码随想录算法训练营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;
    }
};
相关推荐
likexuec3 小时前
数据结构(2)
数据结构
文丑颜不良啊4 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z4 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH5 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶6 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊6 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊6 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊8 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发8 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview