Submit | All submissions | Best solutions | Back to list |
EIUEASPOST - Postorder |
Given a binary tree which has N nodes, write a program to read the tree data, then print all nodes of the tree in the post order (left, right, root)
Input
The first line contains the number of nodes of a tree, N (0 <N ≤ 10^5). The nodes are numbered from 1 to N. The root of the tree is 1.
The i th line of the next N lines contains two integers left and right (left, right ≤ N), respectively, the left child and the right child of the i th node. If left is less than or equal to 0, node i has no children on the left. If right is less than or equal to 0, node i has no right child.
* The tree is guaranteed to exists
Output
All nodes of the tree in the post order, separated by spaces
Example
Input: 4 2 3 0 4 0 0 0 0 Output: 4 2 3 1
Gợi ý
class EIUEASPOST
{
static void Main(string[] args)
{
var nNode = NextInt();
var nodes = ReadTree(nNode);
// You code here
}
static void PrintPostOrder(/*Parameters*/)
{
}
static Node[] ReadTree(int nNode)
{
Node[] nodes = new Node[nNode];
for (var i = 0; i < nNode; i++)
{
nodes[i] = new Node(i + 1);
}
for (var i = 0; i < nNode; i++)
{
var leftIndex = NextInt();
nodes[i].Left = leftIndex > 0 ? nodes[leftIndex - 1] : null;
var rightIndex = NextInt();
nodes[i].Right = rightIndex > 0 ? nodes[rightIndex - 1] : null;
}
return nodes;
}
class Node
{
public int Id;
public Node Left;
public Node Right;
public Node(int id)
{
Id = id;
}
}
}
{
static void Main(string[] args)
{
var nNode = NextInt();
var nodes = ReadTree(nNode);
// You code here
}
static void PrintPostOrder(/*Parameters*/)
{
}
static Node[] ReadTree(int nNode)
{
Node[] nodes = new Node[nNode];
for (var i = 0; i < nNode; i++)
{
nodes[i] = new Node(i + 1);
}
for (var i = 0; i < nNode; i++)
{
var leftIndex = NextInt();
nodes[i].Left = leftIndex > 0 ? nodes[leftIndex - 1] : null;
var rightIndex = NextInt();
nodes[i].Right = rightIndex > 0 ? nodes[rightIndex - 1] : null;
}
return nodes;
}
class Node
{
public int Id;
public Node Left;
public Node Right;
public Node(int id)
{
Id = id;
}
}
}
Added by: | Ha Minh Ngoc |
Date: | 2017-12-10 |
Time limit: | 1s-1.100s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | CSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG FSHARP GO JAVA JS-MONKEY NODEJS PHP PYTHON PYPY PYPY3 PYTHON3 RUBY SQLITE SWIFT VB.NET |