力扣算法刷题 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;
    }
};
相关推荐
计算机安禾1 分钟前
【数据结构与算法】第33篇:交换排序(二):快速排序
c语言·开发语言·数据结构·数据库·算法·矩阵·排序算法
沙雕不是雕又菜又爱玩5 分钟前
leetcode第12、13、14、15题(C++)
c++·算法·leetcode
汀、人工智能11 分钟前
[特殊字符] 第50课:最大路径和
数据结构·算法·数据库架构·图论·bfs·最大路径和
啦啦啦!14 分钟前
项目环境的搭建,项目的初步使用和deepseek的初步认识
开发语言·c++·人工智能·算法
AI成长日志14 分钟前
【笔面试算法学习专栏】链表操作·基础三题精讲(206.反转链表、141.环形链表、21.合并两个有序链表)
学习·算法·面试
算法鑫探17 分钟前
2025 图形(蓝桥杯十六届C组程序题 C 题)
c语言·数据结构·算法·新人首发
田梓燊22 分钟前
leetcode 54
算法·leetcode·职场和发展
wuweijianlove26 分钟前
算法性能优化中的编译器指令重排影响的技术4
算法
沉鱼.4430 分钟前
第十五届题目
linux·运维·算法
我头发多我先学34 分钟前
C++ STL vector 原理到模拟实现
c++·算法