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

(C++二叉树02) 翻转二叉树 对称二叉树 二叉树的深度

226、翻转二叉树

递归法:

交换两个结点可以用swap()方法

class Solution {
public:TreeNode* invertTree(TreeNode* root) {if(root == NULL) return NULL;TreeNode* tem = root->left;root->left = root->right;root->right = tem;invertTree(root->left);invertTree(root->right);return root;}
};

迭代法:

class Solution {
public:TreeNode* invertTree(TreeNode* root) {if(root == NULL) return NULL;stack<TreeNode*> sta;sta.push(root);while(!sta.empty()) {TreeNode* tem= sta.top();sta.pop();swap(tem->left, tem->right);if(tem->right) sta.push(tem->right);if(tem->left) sta.push(tem->left);}return root;}
};

101、对称二叉树

递归法:

class Solution {
public:bool compare(TreeNode* left, TreeNode* right) {if(left == NULL && right != NULL) {return false;}else if(left != NULL && right == NULL) {return false;}else if(left == NULL && right == NULL){return true;}else if (left->val != right->val) {return false;}return compare(left->left, right->right) && compare(left->right, right->left);}bool isSymmetric(TreeNode* root) {if(root == NULL) return true;return compare(root->left, root->right);}
};

104、二叉树的最大深度

递归法:

class Solution {
public:int depth(TreeNode* root) {if(root == NULL) return 0;int left = depth(root->left);int right = depth(root->right);return left > right ? left + 1 : right + 1;}int maxDepth(TreeNode* root) {if(root == NULL) return 0;return depth(root);}
};

迭代法:

class Solution {
public:int maxDepth(TreeNode* root) {if(root == NULL) return 0;queue<TreeNode*> que;int depth = 0;que.push(root);while(!que.empty()) {int size = que.size();depth++;for(int i = 0; i < size; i++) {TreeNode* tem = que.front();que.pop();if(tem->left) que.push(tem->left);if(tem->right) que.push(tem->right);}}return depth;}
};

111、二叉树的最小深度

递归法:

class Solution {
public:int depth(TreeNode* root) {if(root->left == NULL && root->right == NULL) {return 1;}else if(root->left != NULL && root->right == NULL) {return depth(root->left) + 1;}else if(root->left == NULL && root->right != NULL) {return depth(root->right) + 1;}int left = depth(root->left);int right = depth(root->right);return left > right ? right + 1 : left + 1;}int minDepth(TreeNode* root) {if(root == NULL) return 0;return depth(root);}
};

迭代法:

class Solution {
public:int minDepth(TreeNode* root) {if(root == NULL) return 0;queue<TreeNode*> que;int depth = 0;que.push(root);while(!que.empty()) {int size = que.size();depth++;for(int i = 0; i < size; i++) {TreeNode* tem = que.front();que.pop();if(tem->left == NULL && tem->right == NULL) {return depth;}if(tem->left) que.push(tem->left);if(tem->right) que.push(tem->right);}}return depth;}
};

http://www.lryc.cn/news/401699.html

相关文章:

  • 高阶面试-mongodb
  • MySQL数据库慢查询日志、SQL分析、数据库诊断
  • [短笔记] Ubuntu配置环境变量的最佳实践
  • 怎样在 PostgreSQL 中优化对多表关联的连接条件选择?
  • 【Flowable | 第四篇】flowable工作流多任务实例节点实现会签/或签
  • 解决C#读取US7ASCII字符集oracle数据库的中文乱码
  • Linux驱动开发中设备节点、虚拟节点、逻辑节点之间的区别与关系
  • 【iOS】——ARC源码探究
  • ubuntu服务器安装labelimg报错记录
  • Transformer中Decoder的计算过程及各部分维度变化
  • QT实现滑动页面组件,多页面动态切换
  • 使用Python-docx库创建Word文档
  • C# 设计一个可变长度的数据通信协议编码和解码代码。
  • 【MATLAB库函数系列】MATLAB库函数pwelch之功率谱估计的详解及实现
  • 科技出海|百分点科技智慧政务解决方案亮相非洲展会
  • Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
  • Qt窗口程序整理汇总
  • 简单实现一个本地ChatGPT web服务(langchain框架)
  • Elasticsearch-多边形范围查询(8.x)
  • Kotlin Misk Web框架
  • 【设计模式之美】【建造型】工厂模式:通过面向接口编程思路,串起业务流程
  • AI算法19-偏最小二乘法回归算法Partial Least Squares Regression | PLS
  • live555关于RTSP协议交互流程
  • Centos7 安装私有 Gitlab
  • 浅谈数学模型在UGC/AIGC游戏数值配置调参中的应用(AI智能体)
  • 第T5周:使用TensorFlow实现运动鞋品牌识别
  • 网络编程学习之tcp
  • 前端XMLHttpRequest、Fetch API、Axios实现文件上传、下载方法及后端Spring文件服务器处理方法
  • STM32智能交通监测系统教程
  • 【利用Selenium+autoIt实现文件上传】