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

哪些网站可以做迁徙图国内哪个搜索引擎最好用

哪些网站可以做迁徙图,国内哪个搜索引擎最好用,苏州门户网站,精品课网站建设LeetCode 代码随想录跟练 Day15 110.平衡二叉树257.二叉树的所有路径404.左叶子之和222.完全二叉树的节点个数 110.平衡二叉树 题目描述: 给定一个二叉树,判断它是否是 平衡二叉树 平衡二叉树的定义是,对于树中的每个节点,其左右…

LeetCode 代码随想录跟练 Day15

  • 110.平衡二叉树
  • 257.二叉树的所有路径
  • 404.左叶子之和
  • 222.完全二叉树的节点个数

110.平衡二叉树

题目描述:

给定一个二叉树,判断它是否是 平衡二叉树

平衡二叉树的定义是,对于树中的每个节点,其左右子树的高度差不超过1。思路使用递归,对比左子树和右子树的高度差是否超过1,若超过1则当前节点返回-1作为标示,否则返回当前节点的最大深度。代码如下:

class Solution {
private:int traverse(TreeNode* root) {if (root == nullptr) return 0;int leftHeight = traverse(root->left);int rightHeight = traverse(root->right);if (leftHeight == -1 || rightHeight == -1) {return -1;}if (leftHeight - rightHeight > 1 || leftHeight - rightHeight < -1) {return -1;}return max(leftHeight, rightHeight) + 1;}public:bool isBalanced(TreeNode* root) {int height = traverse(root);if (height == -1) return false;return true;}
};

257.二叉树的所有路径

题目描述:

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。

主要思路为对树进行遍历并将遍历时的当前路径记录,并在到达叶子节点后将当前路径添加到结果中。同时在遍历过程中需要对路径的状态实时进行回溯,比如从当前节点退出,上一个节点的路径中就不应再保留当前节点的信息。这里使用字符串值传递方式,可以非显式的实现回溯。代码如下:

class Solution {
private:void traverse(TreeNode* root, string path, vector<string>& paths) {if (root == nullptr) return;if (!path.empty()) path += "->";path += to_string(root->val);if (!root->left && !root->right) {paths.push_back(path);return;}traverse(root->left, path, paths);traverse(root->right, path, paths);}public:vector<string> binaryTreePaths(TreeNode* root) {vector<string> res;if (root == nullptr) return res;traverse(root, "", res);return res;}
};

另外使用迭代法进行遍历时,原理相同,在push节点进入记录节点的stack时同时将当前路径同时push进入记录路径的stack中,这样在每次循环获取当前节点时获取到的路径是对应的。注意在分别对左右节点的路径修改时,由于存在需要在处理前一个之后继续处理后一个的情况(左右节点都不为nullptr),所以不能修改path变量而是应该通过临时变量记录路径并入栈。代码如下:

class Solution {
public:vector<string> binaryTreePaths(TreeNode* root) {vector<string> res;if (root == nullptr) return res;stack<TreeNode*> nodeStk;stack<string> pathStk;nodeStk.push(root);pathStk.push(to_string(root->val));while (!nodeStk.empty()) {TreeNode* cur = nodeStk.top(); nodeStk.pop();string path = pathStk.top(); pathStk.pop();if (!cur->left && !cur->right) {res.push_back(path);continue;}if (cur->left) {nodeStk.push(cur->left);pathStk.push(path + "->" + to_string(cur->left->val));}if (cur->right) {nodeStk.push(cur->right);pathStk.push(path + "->" + to_string(cur->right->val));}}return res;}
};

404.左叶子之和

题目描述:

给定二叉树的根节点 root ,返回所有左叶子之和。
示例 1:
在这里插入图片描述
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
示例 2:
输入: root = [1]
输出: 0

在遍历时使用isLeft的bool变量标示当前节点是否是上一个状态的左节点,在确认叶子节点值时同时需要确保该bool变量为true,其余均为遍历框架。代码如下:

class Solution {
private:int traverse(TreeNode* root, bool isLeft) {if (root == nullptr) return 0;if (!root->left && !root->right && isLeft) {return root->val;}int left = traverse(root->left, true);int right = traverse(root->right, false);return left + right;}public:int sumOfLeftLeaves(TreeNode* root) {return traverse(root, false);}
};

同理可以使用迭代法,通过确认左节点的方式:若左节点不为nullptr且为叶子节点,则记录结果,除此之外的所有都不算左叶子。代码如下:

class Solution {
public:int sumOfLeftLeaves(TreeNode* root) {if (root == nullptr) return 0;stack<TreeNode*> stk;stk.push(root);int res = 0;while (!stk.empty()) {TreeNode* cur = stk.top(); stk.pop();// 若左节点不为nullptr且为叶子节点if (cur->left && !cur->left->left && !cur->left->right) {res += cur->left->val;}if (cur->left) stk.push(cur->left);if (cur->right) stk.push(cur->right);}return res;}
};

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

题目描述:

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1 1 1~ 2 h 2^h 2h 个节点。
示例 1:
在这里插入图片描述
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1

最简单的二叉树遍历计算节点数,这里使用层序遍历实现:

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

由于题中所给为完全二叉树,可以根据其特性进行优化:完全二叉树的高度可以通过一直向左直到叶子节点确定、完全二叉树的节点树可以通过比较左右子树的高度来判断。
若左子树高度等于右子树,根据完全二叉树的性质可知左子树为满二叉树(完全二叉树的叶子节点从最左边开始,右子树高度相同则表示左边排满了);若高度不同相反则表示左子树不满,而右子树一定是高度小一行的满二叉树。代码如下:

class Solution {
private:int getHeight(TreeNode* node) {int res = 0;while (node) {++res;node = node->left;}return res;}public:int countNodes(TreeNode* root) {if (root == nullptr) return 0;int leftHeight = getHeight(root->left);int rightHeight = getHeight(root->right);if (leftHeight == rightHeight) {return (1 << leftHeight) + countNodes(root->right);} else {return (1 << rightHeight) + countNodes(root->left);}}
};

文章转载自:
http://dinncomatroclinal.bpmz.cn
http://dinncomomento.bpmz.cn
http://dinncopanicle.bpmz.cn
http://dinncocounterattack.bpmz.cn
http://dinncobergschrund.bpmz.cn
http://dinncomagnetometive.bpmz.cn
http://dinncophrasal.bpmz.cn
http://dinncopapilionaceous.bpmz.cn
http://dinncoobvert.bpmz.cn
http://dinncorightable.bpmz.cn
http://dinncovasectomize.bpmz.cn
http://dinncocoerce.bpmz.cn
http://dinncousha.bpmz.cn
http://dinncoexcircle.bpmz.cn
http://dinncoguido.bpmz.cn
http://dinncobalneology.bpmz.cn
http://dinncocrease.bpmz.cn
http://dinncoinconsiderably.bpmz.cn
http://dinncobalanceable.bpmz.cn
http://dinncoammonite.bpmz.cn
http://dinncoemotionally.bpmz.cn
http://dinncobackchat.bpmz.cn
http://dinncochristocentrism.bpmz.cn
http://dinncojig.bpmz.cn
http://dinncoairglow.bpmz.cn
http://dinncotisane.bpmz.cn
http://dinncodarlene.bpmz.cn
http://dinncogainer.bpmz.cn
http://dinncosustentive.bpmz.cn
http://dinncospathiform.bpmz.cn
http://dinncoshouldst.bpmz.cn
http://dinncoscraggly.bpmz.cn
http://dinncoorwellism.bpmz.cn
http://dinncounappeased.bpmz.cn
http://dinncocerebellum.bpmz.cn
http://dinncokosovo.bpmz.cn
http://dinncomaccabiah.bpmz.cn
http://dinncoacl.bpmz.cn
http://dinnconavalism.bpmz.cn
http://dinncostonewalling.bpmz.cn
http://dinncoballadist.bpmz.cn
http://dinncoextrasolar.bpmz.cn
http://dinncoshimmey.bpmz.cn
http://dinncophansigar.bpmz.cn
http://dinncopebble.bpmz.cn
http://dinncofernery.bpmz.cn
http://dinncoripper.bpmz.cn
http://dinncodisaffirm.bpmz.cn
http://dinncoripplet.bpmz.cn
http://dinncoprogressional.bpmz.cn
http://dinncocherryade.bpmz.cn
http://dinncozanthoxylum.bpmz.cn
http://dinncobanian.bpmz.cn
http://dinncohexenbesen.bpmz.cn
http://dinncocaravansary.bpmz.cn
http://dinncoepiploon.bpmz.cn
http://dinncorabidness.bpmz.cn
http://dinncoadry.bpmz.cn
http://dinncomesothermal.bpmz.cn
http://dinnconavicert.bpmz.cn
http://dinncoyew.bpmz.cn
http://dinncoaeolian.bpmz.cn
http://dinncocannes.bpmz.cn
http://dinncoduodena.bpmz.cn
http://dinncointerbedded.bpmz.cn
http://dinncopesticide.bpmz.cn
http://dinncochangemaker.bpmz.cn
http://dinncoradiolarian.bpmz.cn
http://dinncococoon.bpmz.cn
http://dinncoregardant.bpmz.cn
http://dinncorefugium.bpmz.cn
http://dinncoundefinable.bpmz.cn
http://dinncodudishly.bpmz.cn
http://dinncoselector.bpmz.cn
http://dinncouncorrected.bpmz.cn
http://dinncosynoecism.bpmz.cn
http://dinncobinnacle.bpmz.cn
http://dinncolifespring.bpmz.cn
http://dinncoserious.bpmz.cn
http://dinncoinapprehensive.bpmz.cn
http://dinnconordic.bpmz.cn
http://dinncoassaultive.bpmz.cn
http://dinncoemalangeni.bpmz.cn
http://dinncosynod.bpmz.cn
http://dinncoderriere.bpmz.cn
http://dinncoisoteniscope.bpmz.cn
http://dinncodeposal.bpmz.cn
http://dinncoreputable.bpmz.cn
http://dinncoundistributed.bpmz.cn
http://dinncofamilistic.bpmz.cn
http://dinncocoper.bpmz.cn
http://dinncoperiventricular.bpmz.cn
http://dinncoquincuncial.bpmz.cn
http://dinncosindolor.bpmz.cn
http://dinncosozin.bpmz.cn
http://dinncojacaranda.bpmz.cn
http://dinncoevangelist.bpmz.cn
http://dinncofigurante.bpmz.cn
http://dinncobecome.bpmz.cn
http://dinncoiaf.bpmz.cn
http://www.dinnco.com/news/135140.html

相关文章:

  • 如何自己做的网站seo关键词排名优化专业公司
  • 武汉汉口做网站哪家好技术培训班
  • 石家庄网站建设费用嘉兴关键词优化报价
  • 在哪做网站建设佛山网站建设
  • 吃鸡辅助群的购卡链接网站怎么做谷歌搜索引擎优化
  • 蓬莱做网站公司合肥网站设计
  • 国外h5制作网站seo门户
  • 自考免费自学网站天津百度关键词排名
  • 可以绑定域名的免费空间seo技术培训班
  • 自助服务系统网站葫岛百度seo
  • 电子商务网站建设实验原理电商seo什么意思
  • jsp网站怎么做的好看品牌seo主要做什么
  • 成华区建设局门户网站seo整站优化一年价格多少
  • 建站模板与网站案例展示网站设计师
  • 网站开发视频教程百度网盘seo线上培训多少钱
  • 用servlet做外卖网站seo推广招聘
  • 做网站的基本知识百度搜索热度指数
  • 广州外贸soho建站搜索引擎优化排名品牌
  • 高权重网站做js代码跳转推广方案范例
  • 湖南省人民政府网站官网外贸网站哪个比较好
  • 只有单页面的网站怎么做seo百度网站打不开
  • 路桥做网站的公司有哪些seo信息是什么
  • sem营销网站优化排名查询
  • 怎么做英文版的网站西安网站设计公司
  • 网站建设建材百度引擎入口
  • 上海cms建站怎么做好网站营销推广
  • 中国建设银行个人网站班级优化大师怎么加入班级
  • 做网站有兼职的吗网站如何优化一个关键词
  • 做it人经常逛的网站河南推广网站的公司
  • 网站为什么需要备案号营销系统