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

树02

226. 翻转二叉树

/*** 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:TreeNode* invertTree(TreeNode* root) {if(root == nullptr) return nullptr;swap(root->left, root->right);invertTree(root->left);invertTree(root->right);return root;}
};

101. 对称二叉树

/*** 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: bool flag = true;bool isSymmetric(TreeNode* root) {if (!root) return true;return isMirror(root->left, root->right);}bool isMirror(TreeNode* t1, TreeNode* t2) {if (!t1 && !t2) return true; // 都是空,对称if (!t1 || !t2) return false; // 只有一个空,不对称if (t1->val != t2->val) return false; // 值不同,不对称// 左的左 和 右的右,左的右 和 右的左 要分别镜像return isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);}
};

104. 二叉树的最大深度

/*** 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:void countDepth(TreeNode *root, int &result, int &count){if(root == nullptr) return;count++;result = max(result, count);if(root->left) {countDepth(root->left, result, count);}if(root->right){countDepth(root->right, result, count);}count--;}int maxDepth(TreeNode* root) {int result = 0, count = 0;countDepth(root, result, count);return result;}
};
int maxDepth(TreeNode* root) {return root == nullptr ? 0 : max(maxDepth(root->left), maxDepth(root->right)) + 1;}

111. 二叉树的最小深度

/*** 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 minDepth(TreeNode* root) {if(root == nullptr) return 0;int leftHeight = minDepth(root->left);int rightHeight = minDepth(root->right);if(root->left && root->right == nullptr)return leftHeight + 1;if(root->left == nullptr && root->right)return rightHeight + 1;elsereturn min(rightHeight, leftHeight)  + 1;}
};
http://www.wuyegushi.com/news/386.html

相关文章:

  • 深入ADC采样
  • 学习笔记:MySQL :eq_range_index_dive_limit参数
  • Python字符串知识点总结
  • SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
  • 读书笔记:Oracle数据库内存结构:系统全局区(SGA)详解
  • 小飞标签
  • 服务器配置的精细化控制(3960)
  • TCP连接优化的实战经验(7340)
  • 家庭主妇人到中年的生活困境很难突破防
  • 中间件架构的优雅实现(0454)
  • 梦醒时分
  • Hyperlane框架最全教学(6165)
  • 并发处理能力的巅峰对决:异步编程的艺术(3501)
  • 实战项目:全栈在线群聊系统(7048)
  • HTTP响应处理的灵活设计(0782)
  • Rust异步Web框架性能突破之路(6359)
  • 服务器配置的精细化控制(7138)
  • 内存使用效率的终极对决:零拷贝技术的实战应用(0345)
  • 明源相关漏洞自查清单(2025)
  • TCP连接优化的实战经验(3513)
  • 异步编程在Web开发中的应用(3842)
  • Proxmox Backup Server 4.0 Beta - 开源企业级备份解决方案
  • Proxmox Mail Gateway 8.2 - 全面的开源邮件安全平台
  • 数控编程利器!Mastercam 2025 安装教程+汉化全流程解析
  • 阿里云AI安全护栏
  • 浅谈一类容量很大但重量很小的背包——2025.7.27 鲜花
  • Centos8搭建hadoop高可用集群
  • 连载小说《Server》 Part 1 《简幻欢》 序言
  • 折腾笔记[30]-使用bun_python通过javascript优雅调用python库
  • Linux系统目录结构完全指南:目录与文件夹的本质区别