- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
对于二叉树,我们定义水平距离如下:
Horizontal distance(hd) of root = 0
If you go left then hd = hd(of its parent)-1, and
if you go right then hd = hd(of its parent)+1.
然后树的底部 View 由树的所有节点组成,其中没有具有相同hd
和更高级别的节点。 (对于给定的 hd
值,可能有多个这样的节点。在这种情况下,它们都属于底部 View 。)我正在寻找一种输出树底部 View 的算法。
例子:
假设二叉树是:
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
树的底 View 是:4 2 5 6 8 7
Ok so for the first example,
Horizontal distance of node with value 1: 0, level = 1
Horizontal distance of node with value 2: 0 - 1 = -1, level = 2
Horizontal distance of node with value 3: 0 + 1 = 1, level = 2
Horizontal distance of node with value 4: -1 - 1 = -2, level = 3
Horizontal distance of node with value 5: -1 + 1 = 0, level = 3
Horizontal distance of node with value 6: 1 - 1 = 0, level = 3
Horizontal distance of node with value 7: 1 + 1 = 2, level = 3
Horizontal distance of node with value 8: 0 + 1 = 1, level = 4
So for each vertical line that is for hd=0, print those nodes which appear in the last level of that line.
So for hd = -2, print 4
for hd = -1, print 2
for hd = 0, print 5 and 6 because they both appear in the last level of that vertical line
for hd = 1, print 8
for hd = 2, print 7
再举一个例子供引用:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
8 9 10 11 12 13 14 15
所以这个的输出将是:8 4 9 10 12 5 6 11 13 14 7 15
Similarly for this example
hd of node with value 1: 0, , level = 1
hd of node with value 2: -1, level = 2
hd of node with value 3: 1, level = 2
hd of node with value 4: -2, level = 3
hd of node with value 5: 0, , level = 3
hd of node with value 6: 0, level = 3
hd of node with value 7: 2, level = 3
hd of node with value 8: -3, level = 4
hd of node with value 9: -1, level = 4
hd of node with value 10: -1, level = 4
hd of node with value 11: 1, level = 4
hd of node with value 12: -1, level = 4
hd of node with value 13: 1, level = 4
hd of node with value 14: 1, level = 4
hd of node with value 15: 3, level = 4
So, the output will be:
hd = -3, print 8
hd = -2, print 4
hd = -1, print 9 10 12
hd = 0, print 5 6
hd = 1, print 11 13 14
hd = 2, print 7
hd = 3, print 15
So the ouput will be:
8 4 9 10 12 5 6 11 13 14 7 15
我已经知道一种方法,我可以使用大量额外空间(一张 map 和一个一维数组来存储该垂直线上最后一个元素的级别)并且时间复杂度为 $O (N\log N)$。这是这个方法的实现:
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
struct Node{
int data;
struct Node *left, *right;
};
Node* newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int height(Node *node)
{
if(node == NULL)
return 0;
else{
int lh = height(node->left);
int rh = height(node->right);
if(lh > rh)
return (lh+1);
else
return (rh+1);
}
}
void printBottom(Node *node, int level, int hd, int min, map< int, vector<int> >& visited, int lev[], int l)
{
if(node == NULL)
return;
if(level == 1){
if(lev[hd-min] == 0 || lev[hd-min] == l){
lev[hd-min] = l;
visited[hd-min].push_back(node->data);
}
}
else if(level > 1)
{
printBottom(node->left, level-1, hd-1, min, visited, lev, l);
printBottom(node->right, level-1, hd+1, min, visited, lev, l);
}
}
void findMinMax(Node *node, int *min, int *max, int hd)
{
if(node == NULL)
return;
if(hd < *min)
*min = hd;
else if(hd > *max)
*max = hd;
findMinMax(node->left, min, max, hd-1);
findMinMax(node->right, min, max, hd+1);
}
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(10);
root->left->right->right = newNode(11);
root->right->left->left = newNode(12);
root->right->left->right = newNode(13);
root->right->right->left = newNode(14);
root->right->right->right = newNode(15);
int min = 0, max = 0;
findMinMax(root, &min, &max, 0);
int lev[max-min+1];
map < int, vector<int> > visited;
map< int,vector<int> > :: iterator it;
for(int i = 0; i < max-min+1; i++)
lev[i] = 0;
int h = height(root);
for (int i=h; i>0; i--){
printBottom(root, i, 0, min, visited, lev, i);
}
for(it = visited.begin() ; it != visited.end() ; it++) {
for(int i=0 ; i < it->second.size() ; i++) {
cout << it->second[i] << " ";
}
}
return 0;
}
我正在寻求帮助以更优化的方式执行此操作,该方式占用的空间或时间更少。这个问题还有其他有效的方法吗?
最佳答案
首先,您可以将时间复杂度降低到 O(n),同时保持相同的空间复杂度。您可以通过在 printBottom
的单次运行中填充 visited
来完成此操作:
void printBottom(Node *node, int level, int hd, int min, map< int, vector<int> >& visited, int lev[])
{
if(node == NULL)
return;
if(lev[hd-min] < level){
lev[hd-min] = level;
visited[hd-min] = new vector<int>; //erase old values, they are hidden by the current node
}
if(lev[hd-min] <= level){
visited[hd-min].push_back(node->data);
}
printBottom(node->left, level+1, hd-1, min, visited, lev);
printBottom(node->right, level+1, hd+1, min, visited, lev);
}
使用初始调用 printBottom(root, 1, 0, min, visited, lev);
如果您坚持按照hd
值递增的顺序输出beig 节点,我认为您无法改善空间消耗。然而,如果你允许不同的输出顺序,你可以摆脱visited
,方法是首先确定'hd'的每个值,应该输出哪个级别,然后再进行一次传递,打印值那场比赛:
void fillLev(Node *node, int level, int hd, int min, int lev[])
{
if(node == NULL)
return;
if(lev[hd-min] < level){
lev[hd-min] = level;
}
fillLev(node->left, level+1, hd-1, min, lev);
fillLev(node->right, level+1, hd+1, min, lev);
}
void printBottom(Node *node, int level, int hd, int min, int lev[])
{
if(node == NULL)
return;
if(lev[hd-min] == level){
cout << node->data;
}
printBottom(node->left, level+1, hd-1, min, lev);
printBottom(node->right, level+1, hd+1, min, lev);
}
通过调用 fillLev(root, 1, 0, min, lev);
和 printBottom(root, 1, 0, min, lev);
。
关于c++ - 打印二叉树的底部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22502322/
我在尝试从子文件夹调用 View 时遇到一些错误。首先,这东西能用 Route::get('/', function() { return View::make('sample'); }); 但是当我
我有另一个 View 设置,并准备好等待其viewmodel。我的RelayCommand到达我的“当前” View 模型。从当前的 View 模型显示新 View 的最佳方法是什么? 我一直在阅读,
我有一个 bigquery View ,我想与数据分析师共享,以便他们可以通过 Data Studio 访问其数据。此共享 View 对另一个数据集中的私有(private) View 进行查询,而私
我有 3 个 View ,并希望将它们集成到一个 View 中,以便它们成为这一 View 中的子文件夹。 我怎样才能做到这一点?还是我必须制作一个 View ,然后再次手动添加和配置这些 View
我在沙发数据库中有一些文档,这些文档的字段是不同关联文档的ID数组: { associatedAssets: ["4c67f6241f4a0efb7dc2abc24a004dfe", "270f
我正在开发一个小实用程序 View ,它将嵌入到我们的几个应用程序中。它将位于一个公共(public)图书馆中。 我应该将其作为 ViewModel 以及默认的 View 实现公开,还是作为具有固定
由于我的某些 View 具有相似的功能,因此我希望能够与每个 View 共享相同的 View 模型。我的想法是将 token 传递给viewmodel的构造函数,但这将导致代码中出现许多if和else
我有一个目标 View (蓝色 View 和红色 View 用于左上角位置)。我试图用手指移动这个 View 。如果 View 不旋转,一切都很好。 但当我旋转 View 并移动时,第一次就很好了。但
我收到这个错误, "Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a
我将发布我目前拥有的源代码,然后解释我的问题。 这是我希望过渡发生的窗口 这是关联的 View 模型 public class MainViewModel {
我正在尝试找出我遇到的错误。最初,我的同事只是使用 将 View 添加到 subview 中 [self.view addSubview:someController.view]; 来自当前ViewC
我是 MVVM 的新手,需要一些帮助。 我的应用程序由许多不同的窗口组成,这些窗口显示允许用户编辑业务层中的数据的控件。 目前,每次用户打开这些窗口之一的新实例时,都会从头开始创建一个 ViewMod
我一直在寻找与我类似的问题以找到解决方案,但我真的找不到类似的东西。 我试图使用 asynctask 类从解析中下载帖子数组,在获取帖子后,它应该在我的页面中设置帖子数组,并执行 setAdapter
这个问题在这里已经有了答案: What is local/remote and no-interface view in EJB? (2 个答案) 关闭 9 年前。 我以前理解它的意思是“接口(in
希望这不会太困惑。 我有一个主视图 Controller ( MainView ),在 View 底部有一个堆栈 View ,在堆栈 View 中我有三个 View 。在一个 View 中(我们称之为
我一直在想这个问题,我真的不知道如何正确地将一个 View Controller 管理的 View 添加到另一个 View Controller 的 View 中。 这不起作用,因为 View 没有完
在明显的情况下,我必须将大量文件从一个 View 复制到另一个 View 。要复制的文件名将作为输入给出。有什么想法可以通过脚本实现吗? 谢谢,日语 最佳答案 最简单的方法是使用 clearfsimp
我正在使用完整日历。这里我的问题是,当单击上一个按钮或下一个按钮单击功能时,如何找到月 View 、周 View 或日 View 格式的完整日历。这里正在调用下一个和上一个按钮的自定义代码。因为使用这
我对这两者感到困惑,并试图找出差异,但没有得到我正在寻找的特定内容。 在哪里使用索引 View 而不是普通 View 。 它们之间的一些重要区别。 最佳答案 关键的区别在于物化 View 很好,物化了
我在一个 xib 中有一个 CustomView,在两个不同的 xib 中有两个不同的 View 。我想在一个 CustomeView 中依次显示这两个 View 。我有一个 NSView 对象,它连
我是一名优秀的程序员,十分优秀!