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

Day15 | 平衡二叉树、二叉树的所有路径、左叶子之和、完全二叉树的节点个数

平衡二叉树

[https://programmercarl.com/0110.平衡二叉树.html#算法公开课]


解法

使用了自顶向下递归,会重复调用find_deepth,时间复杂度较高。还有一种时间复杂度更低的自底向上递归。

class Solution {
public:bool isBalanced(TreeNode* root) {bool res;if(!root) return true;if(find_deepth(root->right) - find_deepth(root->left) > 1 || find_deepth(root->right) - find_deepth(root->left) < -1) return false;return isBalanced(root->left) & isBalanced(root->right);}int find_deepth(TreeNode* root){if(!root) return 0;return max(find_deepth(root->left),find_deepth(root->right)) + 1;}
};

自底向上递归

class Solution {
public:int height(TreeNode* root) {if (root == NULL) {return 0;}int leftHeight = height(root->left);int rightHeight = height(root->right);if (leftHeight == -1 || rightHeight == -1 || abs(leftHeight - rightHeight) > 1) {return -1;} else {return max(leftHeight, rightHeight) + 1;}}bool isBalanced(TreeNode* root) {return height(root) >= 0;}
};

二叉树的所有路径

[https://programmercarl.com/0257.二叉树的所有路径.html#算法公开课]


解法

class Solution {
public:vector<string> binaryTreePaths(TreeNode* root) {vector<string> res;string path = "";find_leaf(root,path,res);return res;}void find_leaf(TreeNode* root,string path,vector<string> &res){if (root->right || root->left){path += to_string(root->val);path += "->";if(root->right) find_leaf(root->right,path,res);if(root->left) find_leaf(root->left,path,res);}else{path += to_string(root->val);res.push_back(path);return;}}
};

左叶子之和

[https://programmercarl.com/0404.左叶子之和.html]


解法

class Solution {
public:int sumOfLeftLeaves(TreeNode* root) {if(!root) return 0;if(!root->right && !root->left) return 0;return find(root->right,0) + find(root->left,1);}int find(TreeNode* root,int sign){if(!root) return 0;if(!root->right && !root->left && sign == 1) return root->val;return find(root->left,1) + find(root->right,0);}
};

完全二叉树的节点个数

[https://programmercarl.com/0222.完全二叉树的节点个数.html#算法公开课]


解法

迭代的层序遍历,记录节点个数

class Solution {
public:int countNodes(TreeNode* root) {if(!root) return 0;queue<TreeNode*> que;que.push(root);int count = 0;while(!que.empty()){root = que.front();que.pop();count++;if(root->right) que.push(root->right);if(root->left) que.push(root->left);}return count;}
};
http://www.jsqmd.com/news/497491/

相关文章:

  • Linux内存管理(78):kcompactd详解
  • Py-Apple Quadruped Robot:低成本开源四足机器人的完整构建指南
  • Python问题总结:关于matplotlib中文字体无法正常显示问题的总结
  • 3.18组会
  • AWS RDS开启审计日志
  • 探索BurpSuite:网络安全测试的瑞士军刀
  • 2026年博士论文10万字怎么降AI?长文降AI的正确打开方式
  • 人工改AI vs 工具降AI:花了8小时和8块钱分别试了一遍
  • Varnish Dashboard: 实时监控和管理Varnish缓存服务器的新利器
  • 微信公众平台测试号的申请与使用
  • 【亲测免费】 TransCoder 项目使用教程
  • 集成开发工具IDEA | Community(社区版,免费)| 试用旗舰版 IntelliJ IDEA 2021.2.2 |历史版本下载 | IDEA全局搜索和替换指定内容,非常方便。
  • 嘎嘎降AI vs 率零 vs 率降:4元价位降AI工具三选一怎么挑
  • REST Client 开源项目教程
  • linuxlinux命令集合
  • 2026年公众号文章被标AI生成怎么办?3款去AI味工具实测推荐
  • YOLOV8训练好的torch模型转换成ONNX、OM格式
  • SuperEasy Local RAG高级配置:自定义Ollama模型与查询优化技巧
  • 探索前沿开发利器:CodeGPT.nvim
  • Jetpack - Room
  • 如何快速部署awesome-DeepLearning:从模型训练到生产环境的完整指南
  • 深度解析SpoofCheck:网络身份验证的新防线
  • 汽车报文中:数据存储的大端序
  • vue截取字符串(商城系统非常常用的小知识)
  • 如何从零开始DIY菠萝狗:Py-Apple Quadruped Robot硬件组装教程
  • 基于JS实现的鸿蒙游戏——二十四点纸牌
  • Alchemy 微服务框架:构建高可用、智能负载均衡的系统
  • 快速汇总公司产品涉及的项目(服务、站点) 查看本机监听的端口 | 查看监听的端口及其关联的服务
  • Py-Apple Dynamics V6.8固件烧录与基础配置完全指南
  • 国产MEMS加速度计怎么选?7家头部企业竞争力分析与应用指南 - 深度智识库