#LCA0000066. Tổ tiên chung của các lá sâu nhất (LCA of Deepest Leaves)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổ tiên chung của các lá sâu nhất (LCA of Deepest Leaves)
LCA of Deepest Leaves
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a rooted binary tree with vertices. A leaf has neither a left nor a right child. Depth is the number of edges from the root. Consider all leaves attaining the maximum depth in the entire tree and output their lowest common ancestor. If there is only one deepest leaf, that leaf is the answer. The PHOJ interface represents the binary tree by explicit left/right child indices instead of LeetCode TreeNode objects.
Input
First line: and root. For each vertex , read , the left and right child indices; means no child.
Output
Print the index of the LCA of all deepest leaves.
Subtasks
General constraints: ; ; ; các chỉ số con khác tạo thành đúng một cây nhị phân; không một đỉnh nào có hai cha.
-
Subtask 1 (20 points): .
-
Subtask 2 (30 points): .
-
Subtask 3 (50 points): .
Examples
Input
5 1
2 3
4 5
0 0
0 0
0 0
Output
2
Explanation
The deepest leaves are and at depth ; their LCA is vertex .