烟台 网站建设淘宝官网首页电脑版登录
news/
2025/9/28 23:30:23/
文章来源:
烟台 网站建设,淘宝官网首页电脑版登录,wordpress 获得分类名称,个人微信crm文章目录 一、题目二、一般遍历解法三、利用完全二叉树性质四、完整代码 所有的LeetCode题解索引#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、一般遍历解法 思路分析#xff1a;利用层序遍历#xff0c;然后用num记录节点数量。其他的例如… 文章目录 一、题目二、一般遍历解法三、利用完全二叉树性质四、完整代码 所有的LeetCode题解索引可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、一般遍历解法 思路分析利用层序遍历然后用num记录节点数量。其他的例如递归法和迭代法也是如此。 层序遍历程序如下
class Solution {
public:int countNodes(TreeNode* root) {if (!root) return 0;queueTreeNode* 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] [2K−1−1,2K−1]之间意味着它只有两种情况一种是满二叉树一种是最后一层叶子节点没有满。对于情况一可以用 2 K − 1 2^K-1 2K−1来计算对于情况二分别递归其左子树和右子树递归到一定深度一定有左子树或者右子树为满二叉树然后按照情况一来计算。那么满二叉树的最左边节点和最右边节点的深度一定是相等的依据这个特性判断子树是否为满二叉树。 递归程序当中我们要确定三个步骤1、输入参数返回值 2、递归终止条件 3、单层递归逻辑。输入参数为中间节点返回值为左子树的节点数量右子树节点数量11是加上中间节点。当节点为空时递归终止返回0。每次递归我们都要计算最左/右边节点深度然后判断二者是否相等如果相等则是满二叉树返回 2 K − 1 2^K-1 2K−1K为深度。程序当中使用了左移运算符因为运算符的优先级问题记得加括号。左移运算符是二进制运算计算机计算的更快。 程序如下
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;queueTreeNode* 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 (vectorstring::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}void my_print2(vectorvectorint v, string str) {cout str endl;for (vectorvectorint::iterator vit v.begin(); vit v.end(); vit) {for (vectorint::iterator it (*vit).begin(); it (*vit).end(); it) {cout *it ;}cout endl;}
}// 前序遍历迭代法创建二叉树每次迭代将容器首元素弹出弹出代码还可以再优化
void Tree_Generator(vectorstring 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); // 右}
}// 层序遍历
vectorvectorint levelOrder(TreeNode* root) {queueTreeNode* que;if (root ! NULL) que.push(root);vectorvectorint result;while (!que.empty()) {int size que.size(); // size必须固定, que.size()是不断变化的vectorint 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()
{vectorstring 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);vectorvectorint tree levelOrder(root);my_print2(tree, 目标树:);Solution2 s1;int result s1.countNodes(root);cout 节点数量为 result endl;system(pause);return 0;
}end
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/921256.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!