给定二叉搜索树,任务是将其按降序整理为排序列表。精确地,每个节点的值必须大于其右侧所有节点的值,并且在展平后其左侧节点必须为NULL。我们必须在O(H)额外空间中执行此操作,其中“ H”是BST的高度。
例子:
Input:
5
/ \
3 7
/ \ / \
2 4 6 8
Output: 8 7 6 5 4 3 2
Input:
1
\
2
\
3
\
4
\
5
Output: 5 4 3 2 1
方法:一个简单的方法是从BST的“反向顺序”遍历中重新创建它。这将占用O(N)的额外空间,其中N是BST中的节点数。
为了对此进行改进,我们将模拟二叉树的反向有序遍历,如下所示:
- 创建一个虚拟节点。
- 创建一个名为“ prev”的变量,使其指向虚拟节点。
- 在每个步骤中执行反向顺序遍历。
- 设置上一个->右=当前
- 设置prev-> left = NULL
- 设置上一个= curr
在最坏的情况下,这将使空间复杂度提高到O(H),因为有序遍历会占用O(H)额外的空间。
下面是上述方法的实现:
C++
// C++ implementation of the
// above approach
#include
using namespace std;
// Node of the binary tree
struct node {
int data;
node* left;
node* right;
node(int data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
// Function to print flattened
// binary tree
void print(node* parent)
{
node* curr = parent;
while (curr != NULL)
cout << curr->data << " ", curr = curr->right;
}
// Function to perform reverse in-order traversal
void revInorder(node* curr, node*& prev)
{
// Base case
if (curr == NULL)
return;
revInorder(curr->right, prev);
prev->left = NULL;
prev->right = curr;
prev = curr;
revInorder(curr->left, prev);
}
// Function to flatten binary tree using
// level order traversal
node* flatten(node* parent)
{
// Dummy node
node* dummy = new node(-1);
// Pointer to previous element
node* prev = dummy;
// Calling in-order traversal
revInorder(parent, prev);
prev->left = NULL;
prev->right = NULL;
node* ret = dummy->right;
// Delete dummy node
delete dummy;
return ret;
}
// Driver code
int main()
{
node* root = new node(5);
root->left = new node(3);
root->right = new node(7);
root->left->left = new node(2);
root->left->right = new node(4);
root->right->left = new node(6);
root->right->right = new node(8);
// Calling required function
print(flatten(root));
return 0;
}
Java
// Java implementation of the
// above approach
import java.util.*;
class GFG{
// Node of the binary tree
static class node
{
int data;
node left;
node right;
node(int data)
{
this.data = data;
left = null;
right = null;
}
};
// Function to print flattened
// binary tree
static void print(node parent)
{
node curr = parent;
while (curr != null)
{
System.out.print(curr.data + " ");
curr = curr.right;
}
}
static node prev;
// Function to perform reverse
// in-order traversal
static void revInorder(node curr)
{
// Base case
if (curr == null)
return;
revInorder(curr.right);
prev.left = null;
prev.right = curr;
prev = curr;
revInorder(curr.left);
}
// Function to flatten binary
// tree using level order
// traversal
static node flatten(node parent)
{
// Dummy node
node dummy = new node(-1);
// Pointer to previous
// element
prev = dummy;
// Calling in-order
// traversal
revInorder(parent);
prev.left = null;
prev.right = null;
node ret = dummy.right;
// Delete dummy node
//delete dummy;
return ret;
}
// Driver code
public static void main(String[] args)
{
node root = new node(5);
root.left = new node(3);
root.right = new node(7);
root.left.left = new node(2);
root.left.right = new node(4);
root.right.left = new node(6);
root.right.right = new node(8);
// Calling required function
print(flatten(root));
}
}
// This code is contributed by Amit Katiyar
C#
// C# implementation of the
// above approach
using System;
class GFG{
// Node of the binary tree
public class node
{
public int data;
public node left;
public node right;
public node(int data)
{
this.data = data;
left = null;
right = null;
}
};
// Function to print flattened
// binary tree
static void print(node parent)
{
node curr = parent;
while (curr != null)
{
Console.Write(curr.data + " ");
curr = curr.right;
}
}
static node prev;
// Function to perform reverse
// in-order traversal
static void revInorder(node curr)
{
// Base case
if (curr == null)
return;
revInorder(curr.right);
prev.left = null;
prev.right = curr;
prev = curr;
revInorder(curr.left);
}
// Function to flatten binary
// tree using level order
// traversal
static node flatten(node parent)
{
// Dummy node
node dummy = new node(-1);
// Pointer to previous
// element
prev = dummy;
// Calling in-order
// traversal
revInorder(parent);
prev.left = null;
prev.right = null;
node ret = dummy.right;
// Delete dummy node
//delete dummy;
return ret;
}
// Driver code
public static void Main(String[] args)
{
node root = new node(5);
root.left = new node(3);
root.right = new node(7);
root.left.left = new node(2);
root.left.right = new node(4);
root.right.left = new node(6);
root.right.right = new node(8);
// Calling required function
print(flatten(root));
}
}
// This code is contributed by Rajput-Ji
输出:
8 7 6 5 4 3 2