原文:

给定一个二叉查找树(bst),任务是以最小-最大方式打印 bst。 什么是 min-max 时尚? 最小-最大方式意味着你必须先打印最大节点,然后最小节点,然后第二个最大节点,然后第二个最小节点,以此类推。

示例:

input:                
         100                            
        /   \    
      20     500     
     /  \                      
    10   30 
          \   
           40
output: 10 500 20 100 30 40
input:
         40                            
        /   \    
      20     50     
     /  \      \               
    10   35     60
        /      /   
      25      55
output: 10 60 20 55 25 50 35 40 

进场:

  1. 创建一个有序数组【】并存储给定二叉查找树的有序遍历。
  2. 由于二叉查找树的有序遍历是按升序排序的,初始化 i = 0j = n–1
  3. 打印以便【i】并更新 i = i 1
  4. 打印以便【j】并更新j = j–1
  5. 重复步骤 3 和 4,直到所有柠檬都被打印出来。

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// structure of each node of bst
struct node {
    int key;
    struct node *left, *right;
};
// a utility function to create a new bst node
node* newnode(int item)
{
    node* temp = new node();
    temp->key = item;
    temp->left = temp->right = null;
    return temp;
}
/* a utility function to insert a new
node with given key in bst */
struct node* insert(struct node* node, int key)
{
    /* if the tree is empty, return a new node */
    if (node == null)
        return newnode(key);
    /* otherwise, recur down the tree */
    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);
    /* return the (unchanged) node pointer */
    return node;
}
// function to return the size of the tree
int sizeoftree(node* root)
{
    if (root == null) {
        return 0;
    }
    // calculate left size recursively
    int left = sizeoftree(root->left);
    // calculate right size recursively
    int right = sizeoftree(root->right);
    // return total size recursively
    return (left   right   1);
}
// utility function to print the
// min max order of bst
void printminmaxorderutil(node* root, int inorder[],
                          int& index)
{
    // base condition
    if (root == null) {
        return;
    }
    // left recursive call
    printminmaxorderutil(root->left, inorder, index);
    // store elements in inorder array
    inorder[index  ] = root->key;
    // right recursive call
    printminmaxorderutil(root->right, inorder, index);
}
// function to print the
// min max order of bst
void printminmaxorder(node* root)
{
    // store the size of bst
    int numnode = sizeoftree(root);
    // take auxiliary array for storing
    // the inorder traversal of bst
    int inorder[numnode   1];
    int index = 0;
    // function call for printing
    // element in min max order
    printminmaxorderutil(root, inorder, index);
    int i = 0;
    index--;
    // while loop for printing elements
    // in front last order
    while (i < index) {
        cout << inorder[i  ] << " "
             << inorder[index--] << " ";
    }
    if (i == index) {
        cout << inorder[i] << endl;
    }
}
// driver code
int main()
{
    struct node* root = null;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
    printminmaxorder(root);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
class gfg
{
// structure of each node of bst
static class node
{
    int key;
    node left, right;
};
static int index;
// a utility function to create a new bst node
static node newnode(int item)
{
    node temp = new node();
    temp.key = item;
    temp.left = temp.right = null;
    return temp;
}
/* a utility function to insert a new
node with given key in bst */
static node insert(node node, int key)
{
    /* if the tree is empty,
    return a new node */
    if (node == null)
        return newnode(key);
    /* otherwise, recur down the tree */
    if (key < node.key)
        node.left = insert(node.left, key);
    else if (key > node.key)
        node.right = insert(node.right, key);
    /* return the (unchanged) node pointer */
    return node;
}
// function to return the size of the tree
static int sizeoftree(node root)
{
    if (root == null)
    {
        return 0;
    }
    // calculate left size recursively
    int left = sizeoftree(root.left);
    // calculate right size recursively
    int right = sizeoftree(root.right);
    // return total size recursively
    return (left   right   1);
}
// utility function to print the
// min max order of bst
static void printminmaxorderutil(node root,
                                 int inorder[])
{
    // base condition
    if (root == null)
    {
        return;
    }
    // left recursive call
    printminmaxorderutil(root.left, inorder);
    // store elements in inorder array
    inorder[index  ] = root.key;
    // right recursive call
    printminmaxorderutil(root.right, inorder);
}
// function to print the
// min max order of bst
static void printminmaxorder(node root)
{
    // store the size of bst
    int numnode = sizeoftree(root);
    // take auxiliary array for storing
    // the inorder traversal of bst
    int []inorder = new int[numnode   1];
    // function call for printing
    // element in min max order
    printminmaxorderutil(root, inorder);
    int i = 0;
    index--;
    // while loop for printing elements
    // in front last order
    while (i < index)
    {
        system.out.print(inorder[i  ]   " "  
                         inorder[index--]   " ");
    }
    if (i == index)
    {
        system.out.println(inorder[i]);
    }
}
// driver code
public static void main(string[] args)
{
    node root = null;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
    printminmaxorder(root);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 implementation of the approach
# structure of each node of bst
class node:
    def __init__(self,key):
        self.left = none
        self.right = none
        self.val = key
def insert(root,node):
    if root is none:
        root = node(node)
    else:
        if root.val < node:
            if root.right is none:
                root.right = node(node)
            else:
                insert(root.right, node)
        else:
            if root.left is none:
                root.left = node(node)
            else:
                insert(root.left, node)
# function to return the size of the tree
def sizeoftree(root):
    if root == none:
        return 0
    # calculate left size recursively
    left = sizeoftree(root.left)
    # calculate right size recursively
    right = sizeoftree(root.right);
    # return total size recursively
    return (left   right   1)
# utility function to print the
# min max order of bst
def printminmaxorderutil(root, inorder, index):
    # base condition
    if root == none:
        return
    # left recursive call
    printminmaxorderutil(root.left, inorder, index)
    # store elements in inorder array
    inorder[index[0]] = root.val
    index[0]  = 1
    # right recursive call
    printminmaxorderutil(root.right, inorder, index)
# function to print the
# min max order of bst
def printminmaxorder(root):
    # store the size of bst
    numnode = sizeoftree(root);
    # take auxiliary array for storing
    # the inorder traversal of bst
    inorder = [0] * (numnode   1)
    index = 0
    # function call for printing
    # element in min max order
    ref = [index]
    printminmaxorderutil(root, inorder, ref)
    index = ref[0]
    i = 0;
    index -= 1
    # while loop for printing elements
    # in front last order
    while (i < index):
        print (inorder[i], inorder[index], end = ' ')
        i  = 1
        index -= 1
    if i == index:
        print(inorder[i])
# driver code
root = node(50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
printminmaxorder(root)
# this code is contributed by sadik ali

c

// c# implementation of the approach
using system;
class gfg
{
// structure of each node of bst
class node
{
    public int key;
    public node left, right;
};
static int index;
// a utility function to create a new bst node
static node newnode(int item)
{
    node temp = new node();
    temp.key = item;
    temp.left = temp.right = null;
    return temp;
}
/* a utility function to insert a new
node with given key in bst */
static node insert(node node, int key)
{
    /* if the tree is empty,
    return a new node */
    if (node == null)
        return newnode(key);
    /* otherwise, recur down the tree */
    if (key < node.key)
        node.left = insert(node.left, key);
    else if (key > node.key)
        node.right = insert(node.right, key);
    /* return the (unchanged) node pointer */
    return node;
}
// function to return the size of the tree
static int sizeoftree(node root)
{
    if (root == null)
    {
        return 0;
    }
    // calculate left size recursively
    int left = sizeoftree(root.left);
    // calculate right size recursively
    int right = sizeoftree(root.right);
    // return total size recursively
    return (left   right   1);
}
// utility function to print the
// min max order of bst
static void printminmaxorderutil(node root,
                                  int []inorder)
{
    // base condition
    if (root == null)
    {
        return;
    }
    // left recursive call
    printminmaxorderutil(root.left, inorder);
    // store elements in inorder array
    inorder[index  ] = root.key;
    // right recursive call
    printminmaxorderutil(root.right, inorder);
}
// function to print the
// min max order of bst
static void printminmaxorder(node root)
{
    // store the size of bst
    int numnode = sizeoftree(root);
    // take auxiliary array for storing
    // the inorder traversal of bst
    int []inorder = new int[numnode   1];
    // function call for printing
    // element in min max order
    printminmaxorderutil(root, inorder);
    int i = 0;
    index--;
    // while loop for printing elements
    // in front last order
    while (i < index)
    {
        console.write(inorder[i  ]   " "  
                      inorder[index--]   " ");
    }
    if (i == index)
    {
        console.writeline(inorder[i]);
    }
}
// driver code
public static void main(string[] args)
{
    node root = null;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
    printminmaxorder(root);
}
}
// this code is contributed by princiraj1992

java 描述语言


output: 

20 80 30 70 40 60 50