Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ List Leaves     - History and Statistics tuptime use tools to view Linux server system boot time (Server)

- Use Docker / LXC quickly launch a desktop system (Linux)

- CentOS 6.6 installation certification system based on the ftp service (Server)

- Start Linux ISO image directly from the hard disk (Linux)

- Analysis: Little Notebook facing a major security threat secure online (Linux)

- MongoDB Learning the notes (Database)

- Linux basic introductory tutorial ---- Software Installation under Linux (Linux)

- Transplant spider to MySQL 5.6 (Database)

- Simple comparison of MySQL and Oracle in a sql analytical details (Database)

- Java object serialization and deserialization (Programming)

- imp / exp Oracle Database import and export commands (Database)

- The Oracle VM VirtualBox use U disk under ubuntu (Linux)

- Through eight skills to let you become a super Linux end-user (Linux)

- Installation Strongswan: on a Linux IPsec-based VPN tool (Linux)

- How to use tmpfs in RHEL / CentOS 7.0 (Linux)

- Enterprise Hadoop cluster architecture - DNS installation (Server)

- Linux System Getting Started Learning: Linux command in w (Linux)

- Linux installation Jetty deployment under RedHat5 8 (Linux)

- Zabbix monitoring tool deployment under Ubuntu server (Server)

- CentOS 6.4 install and configure Squid Proxy Server (Linux)

  List Leaves
  Add Date : 2017-04-13      
  Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case For each case, the first line gives a positive integer N (<= 10) which is the total number of nodes in the tree -. And hence the nodes are numbered from 0 to N-1. . Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node If the child does not exist, a "-" will be put at the position Any pair of children are separated by. a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

1 -
- -
0 -
- -
- -
5 -
Sample Output:


Title to the effect: By entering the number of nodes and the left son and right son of each node, leaf nodes are printed upside down.
Key topics: To understand the first few lines of the input node is to represent the value of a few. Such as sample input in the 0th row 1 - Represents the value of left child node 0 is 1, which points to the first line, the right child is empty (-1)

Code is as follows:

#define N 10

typedef struct Node
    int data, left, right;
} TreeNode;
TreeNode node [N];
TreeNode Queue [N]; // array queue

int first = -1, last = -1;

void Push (TreeNode tn);
TreeNode Pop ();
void printLeaves (int root, int n);

int charToInt (char ch);

int main ()
    int n;
    bool isRoot [N];
    int root;

    scanf ( "% d \ n", & n);
    for (int i = 0; i         isRoot [i] = 1;
    for (int i = 0; i     {
        char cLeft, cRight;
        scanf ( "% c% c", & cLeft, & cRight);
        getchar (); // read carriage buffer zone
        node [i] .left = charToInt (cLeft);
        node [i] .right = charToInt (cRight);
        node [i] .data = i;
        // Node left child and a right child is certainly not the root
        if (node [i] .left! = -1)
            isRoot [node [i] .left] = 0;
        if (node [i] .right! = -1)
            isRoot [node [i] .right] = 0;
    // Find the root
    for (int i = 0; i     {
        if (isRoot [i])
            root = i;
    printLeaves (root, n);

    return 0;

void Push (TreeNode treeNode)
    Queue [++ last] = treeNode;

TreeNode Pop ()
    return Queue [++ first];

// Node tree traversal sequence and print out a leaf node: queue implementation
void printLeaves (int root, int n)
    int leaves [N];
    int k = 0;
    Push (node [root]);
    for (int i = 0; i     {
        TreeNode tn = Pop ();
        // The left child and right child does not exist, it will save the value of the leaf node to the array, formatted for easy printing
        if (tn.left == -1 && tn.right == -1)
            leaves [k ++] = tn.data;
        if (tn.left! = -1)
            Push (node [tn.left]);
        if (tn.right! = -1)
            Push (node [tn.right]);
    for (int i = 0; i         printf ( "% d", leaves [i]);
    printf ( "% d \ n", leaves [k-1]);

int charToInt (char ch)
    if (isdigit (ch))
        return ch - '0';
        return -1;
- Related to optimize the use of Btrfs file system on SSD (Linux)
- Use Visual Studio 2015 to develop Android program (Programming)
- Grading defense against Linux server attacks (Linux)
- Linux kernel to achieve soft RPS network to receive soft interrupt load balancing to distribute (Linux)
- Android design patterns - state mode (Programming)
- Build Eclipse + Maven + Scala-IDEA the Scala Web development environment (Server)
- Ubuntu 14.04 set auto sleep time (Linux)
- Android Studio Installation and Configuration Guide tutorial (Linux)
- Forbid screen change the window size when creating a new window under CentOS (Linux)
- C ++ two second pointer memory model (two-dimensional array) (Programming)
- PostgreSQL procedural language learning (Database)
- Three binary tree traversal (recursive, non-recursive traversal and Morris) (Programming)
- grub boot error resolution examples (Linux)
- Linux system security reinforcement system by masquerading method (Linux)
- PF_RING 6.0.2 compiled and installed on Redhat 6.3 x86_64 (Linux)
- Atheros AR8161 / AR8162 network card driver problem solving in CentOS 6.4 (Linux)
- How to build a container cluster (Server)
- C # / iOS / Android Universal Encryption and decryption (Programming)
- Ubuntu derivative version of the user and how to install SmartGit / HG 6.0.0 (Linux)
- How to disable IPv6 on Ubuntu, Linux Mint, Debian (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.