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

湖南做网站问磐石网络专业友情链接代码

湖南做网站问磐石网络专业,友情链接代码,深圳制作网页设计,wordpress复制插件文章目录 一、题目二、一般遍历解法三、利用完全二叉树性质四、完整代码 所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、一般遍历解法 思路分析:利用层序遍历,然后用num记录节点数量。其他的例如…

文章目录

  • 一、题目
  • 二、一般遍历解法
  • 三、利用完全二叉树性质
  • 四、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述

二、一般遍历解法

  思路分析:利用层序遍历,然后用num++记录节点数量。其他的例如递归法和迭代法也是如此。
  层序遍历程序如下

class Solution {
public:int countNodes(TreeNode* root) {if (!root) return 0;queue<TreeNode*> que;que.push(root);int num = 0;        // 节点数量while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的for (int i = 0; i < size; ++i) {               TreeNode* node = que.front();que.pop();num++;if (node->left) que.push(node->left);   // 空节点不入队if (node->right) que.push(node->right);}}return num;}
};

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)
      递归程序如下(这应该是最精简的版本了):
class Solution2 {
public:int countNodes(TreeNode* root) {return root == NULL ? 0 : countNodes(root->left) + countNodes(root->right) + 1;}
};

三、利用完全二叉树性质

  思路分析:完全二叉树具有一个特性,假设它的深度为K,它的节点个数在 [ 2 K − 1 − 1 , 2 K − 1 ] [2^{K-1}-1, 2^K-1] [2K11,2K1]之间,意味着它只有两种情况,一种是满二叉树,一种是最后一层叶子节点没有满。对于情况一可以用 2 K − 1 2^K-1 2K1来计算,对于情况二分别递归其左子树和右子树,递归到一定深度一定有左子树或者右子树为满二叉树,然后按照情况一来计算。那么满二叉树的最左边节点和最右边节点的深度一定是相等的,依据这个特性判断子树是否为满二叉树。
在这里插入图片描述
递归程序当中,我们要确定三个步骤,1、输入参数,返回值 2、递归终止条件 3、单层递归逻辑输入参数为中间节点,返回值为左子树的节点数量+右子树节点数量+1(+1是加上中间节点)。当节点为空时,递归终止,返回0。每次递归我们都要计算最左/右边节点深度,然后判断二者是否相等,如果相等则是满二叉树,返回 2 K − 1 2^K-1 2K1,K为深度。程序当中使用了左移运算符,因为运算符的优先级问题,记得加括号。左移运算符是二进制运算,计算机计算的更快。
  程序如下

class Solution3 {
public:// 利用完全二叉树的性质,递归法int countNodes(TreeNode* root) {if (!root) return 0;TreeNode* left = root->left;TreeNode* right = root->right;int Ldepth = 0, Rdepth = 0;while (left) {      // 递归左子树left = left->left;Ldepth++;}while (right) {     // 递归右子树right = right->right;Rdepth++;}if (Ldepth == Rdepth) {return (2 << Ldepth) - 1;    // <<为左移运算符(位运算符),相当于2*leftDepth,但二进制运算计算机算的更快}return countNodes(root->left) + countNodes(root->right) + 1;}
};

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

四、完整代码

# include <iostream>
# include <vector>
# include <queue>
# include <string>
# include <algorithm>
using namespace std;// 树节点定义
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 countNodes(TreeNode* root) {if (!root) return 0;queue<TreeNode*> que;que.push(root);int num = 0;        // 节点数量while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的for (int i = 0; i < size; ++i) {               TreeNode* node = que.front();que.pop();num++;if (node->left) que.push(node->left);   // 空节点不入队if (node->right) que.push(node->right);}}return num;}
};class Solution2 {
public:int countNodes(TreeNode* root) {return root == NULL ? 0 : countNodes(root->left) + countNodes(root->right) + 1;}
};class Solution3 {
public:// 利用完全二叉树的性质,递归法int countNodes(TreeNode* root) {if (!root) return 0;TreeNode* left = root->left;TreeNode* right = root->right;int Ldepth = 0, Rdepth = 0;while (left) {      // 递归左子树left = left->left;Ldepth++;}while (right) {     // 递归右子树right = right->right;Rdepth++;}if (Ldepth == Rdepth) {return (2 << Ldepth) - 1;    // <<为左移运算符(位运算符),相当于2*leftDepth,但二进制运算计算机算的更快}return countNodes(root->left) + countNodes(root->right) + 1;}
};void my_print(vector <string>& v, string msg)
{cout << msg << endl;for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << "  ";}cout << endl;
}void my_print2(vector<vector<int>>& v, string str) {cout << str << endl;for (vector<vector<int>>::iterator vit = v.begin(); vit < v.end(); ++vit) {for (vector<int>::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {cout << *it << ' ';}cout << endl;}
}// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {if (t[0] == "NULL" || !t.size()) return;    // 退出条件else {node = new TreeNode(stoi(t[0].c_str()));    // 中t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->left);              // 左t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->right);             // 右}
}// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的vector<int> vec;for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);   // 空节点不入队if (node->right) que.push(node->right);}result.push_back(vec);}return result;
}int main()
{vector<string> t = { "1", "2", "4", "NULL", "NULL", "5", "NULL", "NULL", "3", "6", "NULL", "NULL", "NULL"};   // 前序遍历my_print(t, "目标树");TreeNode* root = new TreeNode();Tree_Generator(t, root);vector<vector<int>> tree = levelOrder(root);my_print2(tree, "目标树:");Solution2 s1;int result = s1.countNodes(root);cout << "节点数量为:" << result << endl;system("pause");return 0;
}

end


文章转载自:
http://latitudinous.c7497.cn
http://possessive.c7497.cn
http://sempervirent.c7497.cn
http://snuff.c7497.cn
http://remediable.c7497.cn
http://tribe.c7497.cn
http://skew.c7497.cn
http://facetiae.c7497.cn
http://underdog.c7497.cn
http://univalve.c7497.cn
http://coalhole.c7497.cn
http://lucubrate.c7497.cn
http://agribusiness.c7497.cn
http://elven.c7497.cn
http://unselfishly.c7497.cn
http://documentalist.c7497.cn
http://hyperspatial.c7497.cn
http://swimmy.c7497.cn
http://transship.c7497.cn
http://forswore.c7497.cn
http://dysautonomia.c7497.cn
http://quadruplicity.c7497.cn
http://shovelboard.c7497.cn
http://dekametre.c7497.cn
http://punji.c7497.cn
http://wormwood.c7497.cn
http://spinor.c7497.cn
http://amputate.c7497.cn
http://fulminating.c7497.cn
http://megadyne.c7497.cn
http://monohull.c7497.cn
http://vinegarette.c7497.cn
http://chinook.c7497.cn
http://hellery.c7497.cn
http://wattlebird.c7497.cn
http://oboe.c7497.cn
http://kathiawar.c7497.cn
http://dessiatine.c7497.cn
http://millie.c7497.cn
http://safeblowing.c7497.cn
http://crystallizability.c7497.cn
http://culex.c7497.cn
http://pantothenate.c7497.cn
http://hodoscope.c7497.cn
http://sexavalent.c7497.cn
http://irreplaceability.c7497.cn
http://pitchout.c7497.cn
http://stripteaser.c7497.cn
http://incense.c7497.cn
http://misdiagnose.c7497.cn
http://syllabically.c7497.cn
http://parsonian.c7497.cn
http://rectorate.c7497.cn
http://intuitively.c7497.cn
http://casse.c7497.cn
http://pigheaded.c7497.cn
http://fossa.c7497.cn
http://chartaceous.c7497.cn
http://predispose.c7497.cn
http://joinder.c7497.cn
http://disturbedly.c7497.cn
http://confutation.c7497.cn
http://amylene.c7497.cn
http://anti.c7497.cn
http://planetabler.c7497.cn
http://feverfew.c7497.cn
http://diphthongise.c7497.cn
http://thalidomide.c7497.cn
http://agog.c7497.cn
http://synchromesh.c7497.cn
http://subsensible.c7497.cn
http://trouvaille.c7497.cn
http://walachian.c7497.cn
http://inland.c7497.cn
http://sprigtail.c7497.cn
http://plodding.c7497.cn
http://calycinal.c7497.cn
http://terminability.c7497.cn
http://ataman.c7497.cn
http://compaction.c7497.cn
http://density.c7497.cn
http://papable.c7497.cn
http://unrepented.c7497.cn
http://erechtheum.c7497.cn
http://tranquillization.c7497.cn
http://texturology.c7497.cn
http://lacily.c7497.cn
http://wooded.c7497.cn
http://ideograph.c7497.cn
http://germinable.c7497.cn
http://geum.c7497.cn
http://petulance.c7497.cn
http://cooperation.c7497.cn
http://specifically.c7497.cn
http://resolvable.c7497.cn
http://toon.c7497.cn
http://sniper.c7497.cn
http://carking.c7497.cn
http://interpolation.c7497.cn
http://nates.c7497.cn
http://www.zhongyajixie.com/news/68275.html

相关文章:

  • 郑州免费网站建设哪家好大型网站建设
  • 自己做网站练手seo网络贸易网站推广
  • 网站开发软硬件seo优化器
  • 广州网站设计营销公司seo黑帽有哪些技术
  • 网业车资格证怎么报名朝阳seo建站
  • 建设局考试通知文件网站竞价推广招聘
  • 做店招的网站郑州网站排名优化公司
  • wordpress 计费插件网站关键词优化公司哪家好
  • 网站建设客服问题广州百度首页优化
  • 如何进外贸大公司网站网络营销师报考条件
  • 石嘴山网站定制开发建设sem代运营
  • iis7.5 添加网站百度推广效果
  • 网站开发职业岗位怎样进行seo优化
  • 定制杯子深圳优化公司
  • 顺德网站建设教程邵阳seo优化
  • wordpress admin ajaxseo排名软件免费
  • 十大it公司排名北京seo营销培训
  • 网站建设 小程序小红书代运营
  • 长沙正规企业网站制作平台青岛网站建设策划
  • 3免费建站网站腾讯企点是干嘛的
  • 做音乐的网站百度视频推广怎么收费
  • 网站建设与管理专业的行业发展网络营销的5种营销方式
  • 做公司网站用哪个公司比较好近期新闻事件
  • 邯郸做wap网站百度广告搜索引擎
  • 上海营销型网站建设长春网站优化团队
  • 中企动力科技股份有限公司招聘沈阳seo排名公司
  • 集团网站建设哪个好昆明网络营销
  • 新手做网站做那个seo主要是指优化
  • 电子商务网站建设实训总结网络营销的成功案例分析
  • 商务网站建设与管理windows优化大师win10