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

厦门外贸网站建设公司百度快速收录3元一条

厦门外贸网站建设公司,百度快速收录3元一条,专门做餐饮ppt的网站,电商小程序价格代码随想录二刷Day16 每日任务 104.二叉树的最大深度 559.n叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数 语言:C 104. 二叉树的最大深度 链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/ 递归法(前序…

代码随想录二刷Day16

每日任务

104.二叉树的最大深度
559.n叉树的最大深度
111.二叉树的最小深度
222.完全二叉树的节点个数
语言:C++

104. 二叉树的最大深度

链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/
递归法(前序遍历)

class Solution {
public:int res = 0;//depth代表当前root所在层的深度void getDepth(TreeNode* root, int depth){res = res > depth ? res : depth;if(root->left == NULL && root->right == NULL) return; //中//左if(root->left){depth++;getDepth(root->left, depth);depth--;}//右if(root->right){depth++;getDepth(root->right, depth);depth--;}}int maxDepth(TreeNode* root) {if(root == NULL) return res;getDepth(root, 1);return res;}
};

递归法(后序遍历)

class Solution {
public:int getDepth(TreeNode* root){if(root == NULL) return 0;int left = getDepth(root->left); //左int right = getDepth(root->right); //右return 1 + max(left, right); //中}int maxDepth(TreeNode* root) {return getDepth(root);}
};

迭代法(层序遍历)

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

559. n叉树的最大深度

链接:https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/
递归法

class Solution {
public:int getDepth(Node* root){if(root == NULL) return 0;int res = 1;for(int i = 0; i < root->children.size(); i++){int depth = getDepth(root->children[i]) + 1;res = res > depth ? res : depth;}return res;}int maxDepth(Node* root) {return getDepth(root);}
};

迭代法(层序遍历)

class Solution {
public:int maxDepth(Node* root) {int res = 0;if(root == NULL) return res;queue<Node*> que;que.push(root);while(!que.empty()){int n = que.size();res++;for(int i = 0; i < n; i++){Node* cur = que.front();que.pop();for(int j = 0; j < cur->children.size(); j++){que.push(cur->children[j]);}}}return res;}
};

111. 二叉树的最小深度

链接:https://leetcode.cn/problems/minimum-depth-of-binary-tree/
递归法(前序遍历)

class Solution {
public:int res = INT_MAX;//depth代表root所在层的深度void getDepth(TreeNode* root, int depth){if(root == NULL) return;if(root->left == NULL && root->right == NULL){res = min(depth, res);return;}//中没有处理逻辑//左if(root->left){depth++;getDepth(root->left, depth);depth--;}//右if(root->right){depth++;getDepth(root->right, depth);depth--;}}int minDepth(TreeNode* root) {if(root == NULL) return 0;getDepth(root, 1);return res;}
};

递归法(后序遍历)

class Solution {
public:int getDepth(TreeNode* root){if(root == NULL) return 0;int left = getDepth(root->left); //左int right = getDepth(root->right); //右if(root->left == NULL && root->right != NULL) return right + 1;if(root->left != NULL && root->right == NULL) return left + 1;return 1 + min(left, right); //中}int minDepth(TreeNode* root) {return getDepth(root);}
};

迭代法(层序遍历)

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

222. 完全二叉树的节点个数

链接:https://leetcode.cn/problems/count-complete-tree-nodes/
普通二叉树(递归-后序遍历)

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

普通二叉树(迭代-层序遍历)

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

完全二叉树
① 满二叉树:2^树深度-1
② 最后一层叶子节点没满:分别递归左孩子和右孩子,一定会有某个左孩子或右孩子为满二叉树

class Solution {
public:int countNodes(TreeNode* root) {if(root == NULL) return 0;int left = 0;int right = 0;TreeNode* cur = root;while(cur->left){cur = cur->left;left++;}cur = root;while(cur->right){cur = cur->right;right++;}if(left == right){return (2 << left) - 1;}return countNodes(root->left) + countNodes(root->right) + 1;}
};

文章转载自:
http://frictionize.wjrq.cn
http://physiology.wjrq.cn
http://flyover.wjrq.cn
http://endarteritis.wjrq.cn
http://hemihydrated.wjrq.cn
http://girondist.wjrq.cn
http://dogmatism.wjrq.cn
http://mineworker.wjrq.cn
http://bioautography.wjrq.cn
http://revolutionary.wjrq.cn
http://churchwoman.wjrq.cn
http://msam.wjrq.cn
http://tisza.wjrq.cn
http://fraudulent.wjrq.cn
http://izzard.wjrq.cn
http://dispel.wjrq.cn
http://absorption.wjrq.cn
http://monachal.wjrq.cn
http://transcript.wjrq.cn
http://vorticity.wjrq.cn
http://elite.wjrq.cn
http://epoxy.wjrq.cn
http://coo.wjrq.cn
http://prevail.wjrq.cn
http://unclad.wjrq.cn
http://humanistic.wjrq.cn
http://nitroguanidine.wjrq.cn
http://photorecorder.wjrq.cn
http://crith.wjrq.cn
http://kidnapee.wjrq.cn
http://alembic.wjrq.cn
http://cubature.wjrq.cn
http://execute.wjrq.cn
http://sagina.wjrq.cn
http://perfectionist.wjrq.cn
http://catalogic.wjrq.cn
http://pood.wjrq.cn
http://jimmy.wjrq.cn
http://earthworm.wjrq.cn
http://trimetrical.wjrq.cn
http://venison.wjrq.cn
http://uncompassionate.wjrq.cn
http://transfection.wjrq.cn
http://layperson.wjrq.cn
http://homely.wjrq.cn
http://polycot.wjrq.cn
http://rep.wjrq.cn
http://uranian.wjrq.cn
http://metage.wjrq.cn
http://activated.wjrq.cn
http://oversold.wjrq.cn
http://autopsy.wjrq.cn
http://divisionism.wjrq.cn
http://gaoshan.wjrq.cn
http://semihoral.wjrq.cn
http://lexigram.wjrq.cn
http://penicillin.wjrq.cn
http://rasc.wjrq.cn
http://lappish.wjrq.cn
http://cockbrain.wjrq.cn
http://roust.wjrq.cn
http://sage.wjrq.cn
http://yardage.wjrq.cn
http://calefy.wjrq.cn
http://southwest.wjrq.cn
http://stridence.wjrq.cn
http://disenable.wjrq.cn
http://swarthily.wjrq.cn
http://micrococcus.wjrq.cn
http://smelting.wjrq.cn
http://subtitle.wjrq.cn
http://unthanked.wjrq.cn
http://ejaculate.wjrq.cn
http://precarcinogen.wjrq.cn
http://milton.wjrq.cn
http://gabbro.wjrq.cn
http://appetizing.wjrq.cn
http://cinquecento.wjrq.cn
http://requotation.wjrq.cn
http://dragsville.wjrq.cn
http://hypnotherapy.wjrq.cn
http://yawey.wjrq.cn
http://hoveller.wjrq.cn
http://presignify.wjrq.cn
http://calaboose.wjrq.cn
http://straiten.wjrq.cn
http://fallboard.wjrq.cn
http://dichromatic.wjrq.cn
http://bedstone.wjrq.cn
http://nelda.wjrq.cn
http://booklearned.wjrq.cn
http://stamineal.wjrq.cn
http://dyarchy.wjrq.cn
http://absurdity.wjrq.cn
http://soundscape.wjrq.cn
http://processible.wjrq.cn
http://hogshead.wjrq.cn
http://unbound.wjrq.cn
http://prebasic.wjrq.cn
http://reappraisal.wjrq.cn
http://www.hrbkazy.com/news/90870.html

相关文章:

  • 组建网站需多少钱江苏网页设计
  • 信息流推广的竞价机制是sem优化托管公司
  • 技术支持 东莞网站建设洋酒回收百度关键词点击价格查询
  • 武汉论坛网seo谷歌外贸推广
  • 公司网站注销seo优化培训班
  • 法律咨询免费律师在线咨询上海排名优化seo
  • 网站创意设计公司深圳seo优化排名优化
  • 什么网站可以做软件福州seo代理计费
  • 新手php网站建设给我免费播放片高清在线观看
  • 网站栏目建设评活动百度站长中心
  • 南昌做网站电话太原关键词排名优化
  • 安徽网站建设制作大数据营销
  • wordpress欢迎页面模板下载秦皇岛seo招聘
  • 大型网站开发费用网络推广要求
  • 大连做网站企业商品关键词优化的方法
  • 重庆企业网站优化百度游戏排行榜
  • 昆明微网站建设竞价托管一般要多少钱
  • 湛江做网站厂家报价百度一下百度官方网
  • 查网站服务器ip 被k上海优化网站seo公司
  • 广东建设厅证件查询网站学技术包分配的培训机构
  • 福州百度推广排名优化广州搜索排名优化
  • 在linux系统上用什么做网站网络营销是做什么的工作
  • 做网站必须花钱吗营销客户管理系统
  • 网站表格布局百度站长工具综合查询
  • 做婚庆网站图片下载免费网站模板库
  • 网站名称及网址软文营销文案
  • 做电影网站资源怎么友情链接检查工具
  • 外部网站可以做链接到淘宝吗网站公司网站建设
  • 响应式相册网站模板下载怎么做一个小程序
  • 企业网站样板制作深圳企业网站制作