三星通信研究院上机试题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/as1072966956/article/details/83117928

You are to find the closest common ancestor of two vertices in a binary tree. For example, the common ancestors of vertices 8 and 13 in the figure below are vertices 3 and 1. Among them, vertex 3 is the closest to the vertex 8 and 13. And the size of sub-tree (the number of vertices in the sub-tree) rooted by vertex 3 is 8.
Given a binary tree and two vertices, write a program that finds the closest common ancestor and computes the size of the sub-tree rooted by the closest common ancestor. No input is given where one of the two given vertices is an ancestor of the other. For example, ‘11 and 3’ in the above tree is an invalid input. Therefore, your program does not have to consider this case.
在这里插入图片描述
[Constraints]
The number of vertices are from 3 to 10000

[Input]
You are given 10 test cases. Each test case has two lines, so the total number of lines is 20. In each test case, the first line consists of four integers, V (the number of vertices in the tree), E (the number of edges), and the indices of two vertices. E edges are listed in the next line. Each edge is represented by two vertices; the index of the parent vertex always precedes the index of the child. For example, the edge connecting vertices 5 and 8 is represented by “5 8”, not by “8 5.” There is no order in which the edges are given. Every consecutive integer in the input is separated by a space.

Given 10 test cases,
First 4 test cases contain small number of vertices(3, 5, 7, 10 each).
Next 6 test cases contain same or greater than 50 vertices.

The indices of vertices are integers from 1 to V, and root vertex always has the index 1.
It is guaranteed that the parent vertex has smaller index than the child vertex.
In this problem, it is not important whether the child is the left child of the parent or the right child; so you can decide this arbitrarily.

[Output]
Output 10 answers in 10 lines. Each line starts with ‘#x’ meaning the index of a test case, and writes the answer after a space. The answer has two integers: the index of the closest common ancestor and the size of the sub-tree rooted by the closest common ancestor. These two integers are separated by a space as well.

[I/O Example]
Input (20 lines in total)
13 12 8 13 ← Start of the first input
1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 10 6 11 11 13
10 9 1 10 ← Start of the second input
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

Output (10 lines in total)
#1 3 8
#2 1 10

package depth_first_search;

/**
 * 
 * 
 * CommonAncestor
 * 本题也可以用haspmap解,一个key-value键值对存放一条边,然后递归向上寻边,即取key寻value,寻到根为止,即找出了到根的路径。
 * 比如第一个测试用例:
 * 13 12 8 13←第一个输入的开始
 * 1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 10 6 11 11 13 
 * 寻完路径获得两个list:
 * list1:8 5 3 1
 * list2:13 11 6 3 1
 * 然后依次调用contains()查list1中的元素是否在list2中出现即可,出现了即要找的,也就是3.
 *
 */
public class CommonAncestor {
	/**
	 *        1
	 *     /    \
	 *    2      3 
	 *     \   /   \
	 *      4 5     6
	 *     / / \   / \
	 *    7 8   9 10  11
	 *   /            /
	 * 12            13
	 * 
	 */
	public static void main(String[] args) {
		Node node11 = new Node(1);
		Node node21 = new Node(2);
		Node node22 = new Node(3);
		node11.left = node21;
		node11.right = node22;
		Node node31 = new Node(4);
		node21.right = node31;
		Node node32 = new Node(5);
		Node node33 = new Node(6);
		node22.left = node32;
		node22.right = node33;
		Node node41 = new Node(7);
		node31.left = node41;
		Node node42 = new Node(8);
		Node node43 = new Node(9);
		node32.left = node42;
		node32.right = node43;
		Node node44 = new Node(10);
		Node node45 = new Node(11);
		node33.left = node44;
		node33.right = node45;
		Node node51 = new Node(12);
		node41.left = node51;
		Node node52 = new Node(13);
		node45.left = node52;
		System.out.println(LCA(node11, node32, node52).val);
	}

	public static class Node {
		int val;
		Node left;
		Node right;

		Node(int x) {
			val = x;
		}
	}

	public static Node LCA(Node root, Node a, Node b) {
		if (root == null) {
			return null;
		}
		// If the root is one of a or b, then it is the LCA
		if (root == a || root == b) {
			return root;
		}
		Node left = LCA(root.left, a, b);
		Node right = LCA(root.right, a, b);
		// If both nodes lie in left or right then their LCA is in left or right,
		// Otherwise root is their LCA
		if (left != null && right != null) {
			return root;
		}
		return (left != null) ? left : right;
	}
}

猜你喜欢

转载自blog.csdn.net/as1072966956/article/details/83117928
今日推荐