力扣算法刷题 Day 15

110. 平衡二叉树

题目链接

110. 平衡二叉树 - 力扣(LeetCode)

思路

首先需要明确,这道题是在考察平衡二叉树的定义。任何一个节点左右子树的高度相差不超过1则为平衡二叉树。联想到最大深度的解法,我们选择递归法来做,返回值为int,入口参数为节点,结束条件是当前节点为空,则返回0.那么如何标记非平衡二叉树呢?如果已经不满足了,则让它返回-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 {
public:
    int getheight(TreeNode *node)
    {
        if(node == NULL) return 0;
        int lh = getheight(node->left);
        if(lh == -1)    return -1;
        int rh = getheight(node->right);
        if(rh == -1)    return -1;
        int n = abs(rh - lh);
        if(n <= 1) return 1 + max(rh,lh);
        else return -1;   
    }
    bool isBalanced(TreeNode* root) {
        int res = getheight(root);
        if(res >= 0) return true;
        else return false; 
    }
};

257. 二叉树的所有路径

题目链接

257. 二叉树的所有路径 - 力扣(LeetCode)

思路

找到所有路径,我们需要回溯。在找到一条路径走到底时,跳转至它的父节点。选择递归方法来做时,函数返回值为void,入口参数需要当前节点,已经走过的节点栈(便于回溯,需要回溯时栈顶出栈即可),结果集(保存路径)。结束递归的条件是,当前节点为叶子节点。 递归内部实现:首先当前节点加入结果集,然后将其入栈,去找左子树的路径;完成后找右子树的路径,最后返回结果集。

需要注意:左子树和右子树在递归前,需要先判断是否有子节点。以及回溯和递归要写在一起。

文章详解

257. 二叉树的所有路径 | 代码随想录

cpp 复制代码
class Solution {
private:

    void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {
        path.push_back(cur->val); 
        
        if (cur->left == NULL && cur->right == NULL) {
            string sPath;
            for (int i = 0; i < path.size() - 1; i++) {
                sPath += to_string(path[i]);
                sPath += "->";
            }
            sPath += to_string(path[path.size() - 1]);
            result.push_back(sPath);
            return;
        }
        if (cur->left) { // 左 
            traversal(cur->left, path, result);
            path.pop_back(); // 回溯
        }
        if (cur->right) { // 右
            traversal(cur->right, path, result);
            path.pop_back(); // 回溯
        }
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if (root == NULL) return result;
        traversal(root, path, result);
        return result;
    }
};

404. 左叶子之和

题目链接

404. 左叶子之和 - 力扣(LeetCode)

思路

明确什么是左叶子之和:所有的左叶子节点之和。怎么判断呢?父节点有左孩子,且该左孩子的左右子节点为空。之后遍历就可以了。

文章详解

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 == NULL)    return 0;
        if(root->left == NULL && root->right == NULL) return 0;
        int leftvalue = sumOfLeftLeaves(root->left);
        if(root->left && root->left->left == NULL && root->left->right == NULL)
        {
            leftvalue = root->left->val; 
        }
        int rightvalue = sumOfLeftLeaves(root->right);

        int sum = leftvalue + rightvalue;
        return sum;
    }
};

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

题目链接

222. 完全二叉树的节点个数 - 力扣(LeetCode)

思路

迭代法层序遍历。如果要优化,想法是计算层数,最后一层之前的不需要遍历,每一层节点个数为2^(n-1),只需要遍历最后一层。

文章详解

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) {
        queue <TreeNode*> que;
        int res = 0;
        if(root == NULL) return res;
        que.push(root);
        while(!que.empty())
        {
            int size = que.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* cur = que.front();
                que.pop();
                res++;
                if(cur->left)   que.push(cur->left);
                if(cur->right)  que.push(cur->right);
            }
        }
        return res;
    }
};
相关推荐
程序员小崔日记2 小时前
一道基础计算题卡在 40 分,求助判题规则问题
java·算法·竞赛
愣头不青2 小时前
543.二叉树的直径
java·算法
此方ls2 小时前
机器学习聚类算法二——DBSCAN(Density-Based Spatial Clustering of Applications with Noise)
算法·机器学习·聚类
add45a2 小时前
C++中的原型模式
开发语言·c++·算法
2401_844221322 小时前
C++类型推导(auto/decltype)
开发语言·c++·算法
2201_753877792 小时前
高性能计算中的C++优化
开发语言·c++·算法
hans汉斯2 小时前
基于区块链和语义增强的科研诚信智能管控平台
人工智能·算法·yolo·数据挖掘·区块链·汉斯出版社
2501_945425152 小时前
分布式系统容错设计
开发语言·c++·算法
冷小鱼2 小时前
机器学习极简入门:从外卖预测到AI核心算法
人工智能·算法·机器学习