力扣算法刷题 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;
    }
};
相关推荐
wuweijianlove6 分钟前
算法的平均复杂度建模与性能回归分析的技术7
算法·数据挖掘·回归
子琦啊10 分钟前
【算法复习】字符串 | 两个底层直觉,吃透高频题
linux·运维·算法
code_pgf2 小时前
Octo 算法详解-开源通用机器人策略模型技术报告
算法·机器人·开源
嘻嘻哈哈樱桃2 小时前
牛客经典101题题解集--动态规划
java·数据结构·python·算法·职场和发展·动态规划
脱氧核糖核酸__2 小时前
LeetCode热题100——234.回文链表(两种解法)
c++·算法·leetcode·链表
IronMurphy2 小时前
【算法四十二】118. 杨辉三角 198. 打家劫舍
算法
电科一班林耿超2 小时前
第 16 课:动态规划专题(二)—— 子序列与子数组问题:面试最高频的 DP 题型
数据结构·算法·动态规划
生信研究猿2 小时前
leetcode 416. 分割等和子集
算法·leetcode·职场和发展
狗哥哥3 小时前
面包屑自动推导的算法设计:从“最短路径匹配”到工程可落地
算法·架构