Tree height (no need to build a tree, no need to consider who is the child, because the input ensures the parent node)

Source: Internet
Author: User

Description
Now there is a valid binary tree. the nodes in the tree are represented by numbers. Now, given all the parent-child relationships on the tree, Please calculate the height of the tree.
Input description:

The first line indicates the number of nodes N (1 ≤ n ≤ 1000, and the number of nodes is 0 to n-1,
There are n-1 rows. Each row has two integers. The first number indicates the number of the parent node, and the second number indicates the number of the child node.

Output description:

The height of the output tree, which is an integer.

Example 1
Input

5
0 1
0 2
1 3
1 4

Output

3

# Import sys # sys. stdin = open('input.txt ', 'R') n = int (input () tree = [1 For I in range (n)] childnum = [0 for I in range (n)] For I in range (n-1): parent, child = map (INT, input (). split () If childnum [Parent]> = 2: tree [child] = 0 childnum [child] = 2 continue tree [child] + = tree [Parent] childnum [Parent] + = 1 print (max (tree ))

Tree height (no need to build a tree, no need to consider who is the child, because the input ensures the parent node)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.