当前位置: 首页 > news >正文

算法打卡 Day19(二叉树)-平衡二叉树 + 二叉树的所有路径 + 左叶子之和 + 完全二叉树的节点个数

Leetcode 101-平衡二叉树

文章目录

  • Leetcode 101-平衡二叉树
    • 题目描述
    • 解题思路
  • Leetcode 257-二叉树的所有路径
    • 题目描述
    • 解题思路
  • Leetcode 404-左叶子之和
    • 题目描述
    • 解题思路
  • Leetcode 222-完全二叉树的节点个数
    • 题目描述
    • 解题思路

题目描述

https://leetcode.cn/problems/balanced-binary-tree/description/

在这里插入图片描述

解题思路

二叉树节点的深度是指从根节点到该节点的最长简单路径边的条数。

二叉树节点的高度是指从该节点到叶子节点的最长简单路径边的条数。

这道题我们使用递归,采用后序遍历的方法,不断获得左右节点的高度,并在中间节点比较其高度是否符合平衡二叉树的要求

class Solution {
public:int getHight(TreeNode* root) {if (root == nullptr) return 0;int leftHeight = getHight(root->left);if (leftHeight == -1) return -1;int rightHeight = getHight(root->right);if (rightHeight == -1) return -1;int result;if (abs(leftHeight - rightHeight) > 1) result = -1;else result = 1 + max(leftHeight , rightHeight); return result;}bool isBalanced(TreeNode* root) {return getHight(root) == -1 ? false : true;}
};

Leetcode 257-二叉树的所有路径

题目描述

https://leetcode.cn/problems/binary-tree-paths/description/

在这里插入图片描述

解题思路

采用前序算法依次遍历

class Solution {
public:void tranversal(TreeNode* cur, vector<int>& path, vector<string>& result) {path.push_back(cur->val);//中if (cur->left == nullptr && cur->right == nullptr) {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) {tranversal(cur->left, path, result);path.pop_back();//回溯}if (cur->right) {tranversal(cur->right, path, result);path.pop_back();//回溯}}vector<string> binaryTreePaths(TreeNode* root) {vector<string>result;vector<int>path;if (root == nullptr)return result;tranversal(root, path, result);return result;}
};

Leetcode 404-左叶子之和

题目描述

在这里插入图片描述

解题思路

叶子节点的左右子节点都为 nullptr,左叶子节点指的是该叶子节点是父节点的左节点。

采用递归后序遍历的方式解决:

class Solution {
public:int sumOfLeftLeaves(TreeNode* root) {if (root == nullptr) return 0;if (root->left == nullptr && root->right == nullptr) return 0;int leftValue = sumOfLeftLeaves(root->left);//左if (root->left && root->left->left == nullptr && root->left->right == nullptr) leftValue = root->left->val;int rightValue = sumOfLeftLeaves(root->right);//右int sum = leftValue + rightValue;//中return sum;}
};

Leetcode 222-完全二叉树的节点个数

题目描述

在这里插入图片描述

解题思路

完全二叉树的定义是:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1-2^h 个节点。

不考虑完全二叉树的特性,仅将其当作普通二叉树,采用后序遍历的代码为:

class Solution {
public:int countNodes(TreeNode* root) {if (root == nullptr) return 0;int leftNode = countNodes(root->left);int rightNode = countNodes(root->right);int sum = leftNode + rightNode + 1;return sum;}
};

此时我们将所有节点都遍历了一遍,因此时间复杂度为 O ( n ) O(n) O(n)

为了降低时间复杂度,我们可以利用完全二叉树的特性,即对于满二叉树,其节点个数为(2^n-1),在遍历过程中仅仅遍历两侧的节点,从而可以降低时间复杂度

class Solution {
public:int countNodes(TreeNode* root) {if (root == nullptr) return 0;TreeNode* left = root->left;TreeNode* right = root->right;int leftDepth = 1, rightDepth = 1;while (left) {//遍历左侧深度left = left->left;leftDepth++;}while (right) {//遍历右侧深度right = right->right;rightDepth++;}if (leftDepth == rightDepth)return (pow(2, leftDepth) - 1);//如果为满二叉树则根据公式直接计算节点个数int leftNum = countNodes(root->left);int rightNum = countNodes(root->right);int sum = leftNum + rightNum + 1;return sum;}
};
http://www.lryc.cn/news/420575.html

相关文章:

  • 国际以太网专线 (IEPL)/国际专线(IPLC)-全球覆盖,无界沟通
  • 信息安全管理知识体系攻略(至简)
  • HCIE学习笔记:IPV6 地址、ICMP V6、NDP 、DAD (更新补充中)
  • 人工智能】Transformers之Pipeline(九):物体检测(object-detection)
  • [SWPUCTF 2021 新生赛]easy_md5
  • Redis面试题大全
  • 【langchain学习】BM25Retriever和FaissRetriever组合 实现EnsembleRetriever混合检索器的实践
  • 【C语言】预处理详解(上)
  • uni-app内置组件(基本内容,表单组件)()二
  • linux搭建redis超详细
  • Flink-DataWorks第二部分:数据集成(第58天)
  • 4个从阿里毕业的P7打工人,当起了包子铺的老板
  • javaweb_07:分层解耦
  • 调用 Python 开源库,获取油管英文视频的手动或自动英文srt字幕,以及自动中文简体翻译srt字幕
  • UDP协议实现通信与数据传输(创建客户端和服务器)
  • 【红黑树】
  • 排序算法——简单选择排序
  • OpenAI API推出结构化输出功能
  • Python 异步编程:Sqlalchemy 异步实现方式
  • 父类引用指向子类对象
  • 分享一个基于Spring Boot的面向社区的智能化健康管理系统的设计与实现(源码、调试、LW、开题、PPT)
  • 【扒代码】reduction参数是什么
  • Python,Spire.Doc模块,处理word、docx文件,极致丝滑
  • redis的安装与命令
  • 【C++】特殊类设计类型转换
  • 为git 命令行 设置代理环境变量
  • 自定义linux某些常见配置
  • 告别手动操作!KeyMouseGo实现自动化工作流
  • 大型语言模型微调 新进展-4篇 论文
  • 专业课140+杭电杭州电子科技大学843信号与系统考研经验电子信息与通信工程真题,大纲,参考书。