Post-Order Traversal of a Tree#4038

Post-Order Traversal of a Tree

1 second
122.174 megabytes
Very easy

Given an array of integers. Create a Binary Search Tree from these numbers. If the inserted value equals to the current node, insert it to the right subtree.

Write method PostOrder that makes Post-Order traversal of a tree. In this traversal method the left subtree is visited first, then the right subtree and finally the root node.

prb7463.gif

Write the code according to the next interface:

class TreeNode
{
public:
    int val;
    TreeNode *left, *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Tree
{
public:
    TreeNode *head;
    Tree() : head(NULL) {};
    void Insert(int val); // Insert number val to Binary Search Tree
    void PostOrder(void); // Print the vertices of a tree according to post-order traversal
};
class TreeNode:

    def __init__(self, x: int):
        self.val = x
        self.left = None
        self.right = None

class Tree:

    def __init__(self):
        self.head = None

    def Insert(self, val: int) -> None:
        """Insert number val to Binary Search Tree"""
        pass

    def PostOrder(self) -> None:
        """Print the vertices of a tree according to post-order traversal"""
        pass

You can create (use) additional methods if needed.

Input

The first line contains the number (1 \leq n \leq 100). The second line contains integers.

Output

Create the Binary Search Tree from the input data. Print the vertices of the tree according to post-order traversal.

Examples

Example 1

Loading
Loading