- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想将二叉树存储在数组中,在缺失的节点处为空值。
例如:
输入:
1
/ \
2 3
/ \ \
4 5 6
输出:{1,2,3,4,5,0,6}
我已经尝试对数组中的二叉树进行线性遍历,但我希望在树的缺失节点位置为 null。
std::vector< int > levelorder( tree *root){
queue<tree*> q;
tree* temp;
q.push(root);
while(!q.empty()){
temp=q.front();
q.pop();
arr.push_back(temp->data);
if(temp->left && temp->right)
{
q.push(temp->left);
q.push(temp->right);
}
else if(temp->left && !temp->right)
{
q.push(temp->left);
arr.insert(0);
}
else if(temp->right && !temp->left)
{
q.push(temp->right);
arr.push_back(0);
}
}
return arr;
}
int main()
{
tree *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
cout<<"Level Order traversal of binary tree is :"<<endl;
levelorder(root);
for(int i =0; i<arr.size(); i++)
{
cout<< arr[i]<<" ";
}
return 0;
}
我得到输出:{1,2,3,0,4,5,6}但我希望输出为:{1,2,3,4,5,0,6}
最佳答案
以下代码支持丢失子树,并在不存在有意义的节点时立即停止:
std::vector<int> levelorder(tree *root){
int null_in_queue = 0;
std::queue<tree*> q;
std::vector<int> arr;
q.push(root);
while(q.size() != null_in_queue){
tree* temp=q.front();
q.pop();
if(!temp)
{
arr.push_back(0);
q.push(nullptr);
q.push(nullptr);
null_in_queue++; // One was removed, two were added
}
else
{
arr.push_back(temp->data);
q.push(temp->left);
q.push(temp->right);
if (!temp->left) null_in_queue++;
if (!temp->right) null_in_queue++;
}
}
return arr;
}
它将虚拟节点(nullptr)插入队列并统计虚拟节点的数量。当只剩下虚拟节点时,它终止。
该算法的时间和内存复杂度为O(n)
,其中n
是输出数组的大小。这个数组的大小最多是一棵完整树的节点数(减一),直到最深的节点。对于深度为 d
的树,该算法的复杂度为 O(2^d)
。
详情:循环中的代码是常数(摊销的)并且q
vector 包含节点和虚拟节点,其中最多包含2^(d+1)个节点(额外的一行虚拟节点将在算法完成之前添加)。这意味着整个算法将在 O(2^(d+1)) ~ O(2*2^d) ~ O(2^d)
内执行。
关于c++ - 将二叉树存储在缺失位置为空值的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024455/
我编写了一个 Android 应用程序,它使用 Azure 来执行用户通过 Google、Twitter 和 Facebook 的登录;它使用 Microsoft.WindowsAzure.Mobil
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我正在学习“绘图应用程序”的教程。当我在 Firefox 上启动我的应用程序时,Firebug 告诉我“在语句之前缺少 ;” 我在第 9 行调用函数的位置。我只是不明白应该将这些“;”放在哪里. va
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我在 Firebug 中不断收到关于 onClick 事件的错误。 我已经尝试了 "和 ' 的各种不同组合,但无济于事。在添加 onClick 事件之前,这工作正常。 有人能发现我可能做错了什么吗?
Visual Studio 2015 告诉我找不到 WSASetSocketSecurity。 该 dll 存在并且还包括似乎没问题。 我的包括: windows.h stdio.h Wincrypt
我需要访问 eloquent 的 whereHasNot方法(此处添加: https://github.com/laravel/framework/commit/8f0cb08d8ebd157cbfe
跟随宠物物体检测的 TF 教程:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/run
构建路径 > 添加库 > JUnit 无法添加 JUnit3 或 JUnit4 组件。 我在.log 中看到这样的消息 !MESSAGE No property tester contributes
我正在运行此脚本来查看网络上的摄像机: gst-launch udpsrc port=1234 ! "application/x-rtp, payload=127" ! rtph264depay !
我正在使用http://java.sun.com/jsp/jstl/fmt用于从 Spring 配置中设置的 Message Resource Bundle 输出消息的标签库。消息解析也可以放在 Co
我正在将 Ninject 与 MVC4 连接起来,并让它工作到尝试实际解决依赖关系的程度。但是,我收到以下异常: Method not found: 'System.Web.Http.Services
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我在启动 ASP.NET MVC5 应用程序时遇到问题。到目前为止一切正常。启动应用程序时出现以下错误: Could not load file or assembly 'Microsoft.Appl
我已经使用以下方法创建了一个环境: conda create --prefix C:\Users\Dell\Dropbox\DjangoProjects\webenv python=3.6 执行后:c
我们有一个遗留的 Web 窗体应用程序,我们最近将其从网站项目转换为 Web 应用程序项目。 Web 窗体项目是解决方案的“启动”项目。 有一个 MVC 项目是对 Web 窗体项目的引用。 在 MVC
使用某种字体,我使用Java的FontLayout来确定它的上升、下降和行距。 (参见 Java 的 FontLayout 教程 here) 在我的具体案例中,我使用的是 Arial Unicode
我正在尝试在 linux 下编译 qt ffmpeg 包装器简单编码/解码示例 QTFFmpegWrapper source # Set list of required FFmpeg librari
我正在使用来自开发人员 android 页面的 SlidingTabLayout.java。在我使用 slidingTabLayout.setDistributeEvenly(true); 使 sli
我正在尝试使用 v360 filter 将 180° 鱼眼视频转换为普通/常规视频的 FFmpeg . 这是我尝试过的命令:ffmpeg -i in.mp4 -vf "v360=input=fishe
我是一名优秀的程序员,十分优秀!