-
题目地址:http://ac.jobdu.com/problem.php?pid=1509
-
给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先。
题目描述:
-
输入:
-
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数。
其中每个测试样例包括两行,第一行为一个二叉树的先序遍历序列,其中左右子树若为空则用0代替,其中二叉树的结点个数node_num<10000。
第二行为树中的两个结点的值m1与m2(0<m1,m2<10000)。
-
输出:
-
对应每个测试案例,
输出给定的树中两个结点的最低公共祖先结点的值,若两个给定结点无最低公共祖先,则输出“My God”。
-
样例输入:
-
2 1 2 4 6 0 0 7 0 0 5 8 0 0 9 0 0 3 0 0 6 8 1 2 4 6 0 0 7 0 0 5 8 0 0 9 0 0 3 0 0 6 12
-
样例输出:
-
2 My God
基本思路:
建立二叉树。
建好树之后下一步计算路径。
比较两个路径获取最低公共节点。
C++ AC
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
using namespace std;
int n,i;
struct Node{
int data;
Node *left;
Node *right;
};
Node *createTree(){
int data;
scanf("%d",&data);
if(data == 0){
return NULL;
}
Node *node = new Node;
node->data =data;
node->left = createTree();
node->right = createTree();
return node;
}
bool getPath(int m,Node *root,vector<int> &p){
if(root==NULL){
return false;
}
p.push_back(root->data);
if(root->data != m){
if(getPath(m,root->left,p)||getPath(m,root->right,p)){
return true;
}else{
p.pop_back();
return false;
}
}else{
return true;
}
}
int main(){
scanf("%d",&n);
while(n > 0){
n--;
Node *root = createTree();
int m1,m2;
scanf("%d%d",&m1,&m2);
vector<int> p1,p2;
if(getPath(m1,root,p1)&&getPath(m2,root,p2)){
int len1 = p1.size();
int len2 = p2.size();
int len = len1 > len2 ? len2 : len1;
i = 0;
while(i < len){
if(p1[i] != p2[i]){
break;
}
i++;
}
printf("%d\n",p1[i-1]);
}else{
printf("My God\n");
}
}
return 0;
}
/**************************************************************
Problem: 1509
User: wangzhenqing
Language: C++
Result: Accepted
Time:130 ms
Memory:5348 kb
****************************************************************/
Java AC
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Pattern;
public class Main {
/*
* 1509
*/
private static int[] inputArr;
private static int k ;
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
while (n > 0) {
n --;
String input = scanner.nextLine();
String inputMsg[] = input.split(Pattern.quote(" "));
int len = inputMsg.length;
inputArr = new int[len];
List<Integer> numList = new ArrayList<Integer>();
for (int i = 0; i < len; i++) {
int tempNum = Integer.parseInt(inputMsg[i]);
numList.add(tempNum);
inputArr[i] = tempNum;
}
String m1m2 = scanner.nextLine();
String m2m2Msg[] = m1m2.split(Pattern.quote(" "));
int m1 = Integer.parseInt(m2m2Msg[0]);
int m2 = Integer.parseInt(m2m2Msg[1]);
if (m1 == 0) {
System.out.println("My God");
continue;
}
if (m2 == 0) {
System.out.println("My God");
continue;
}
if (!numList.contains(m1)) {
System.out.println("My God");
continue;
}
if (!numList.contains(m2)) {
System.out.println("My God");
continue;
}
k = 0;
BinaSortTree biTree = createTree();
List<Integer> charList1 = new ArrayList<Integer>();
List<Integer> charList2 = new ArrayList<Integer>();
getPath(biTree , m1 ,charList1);
getPath(biTree , m2 ,charList2);
charList1.add(m1);
charList2.add(m2);
getCommonList(charList1 ,charList2);
}
}
private static void getCommonList(List<Integer> charList1,
List<Integer> charList2) {
int result = 0;
int rank = 0;
int size1 = charList1.size();
int size2 = charList2.size();
int size = Math.min(size1, size2);
while (rank < size) {
int value1 = charList1.get(rank);
int value2 = charList2.get(rank);
if (value1 == value2) {
result = value1;
}
rank++;
}
System.out.println(result);
}
private static void getPath(BinaSortTree biTree, int m, List<Integer> charList ) {
Stack<Integer> stack = new Stack<Integer>();
getActualPath(biTree ,stack , m ,charList);
}
private static void getActualPath(BinaSortTree biTree,
Stack<Integer> stack, int m, List<Integer> charList) {
if (biTree != null) {
if (biTree.getValue() == m) {
for (int actualm : stack) {
charList.add(actualm);
}
return;
}
stack.push(biTree.getValue());
getActualPath(biTree.getLchild(), stack, m, charList );
getActualPath(biTree.getRchild(), stack, m, charList );
stack.pop();
}
}
private static BinaSortTree createTree() {
int node = inputArr[k];
k++;
if (node == 0) {
return null;
}
BinaSortTree biTree = new BinaSortTree(node);
biTree.setLchild(createTree());
biTree.setRchild(createTree());
return biTree;
}
private static class BinaSortTree {
private BinaSortTree lchild;
private BinaSortTree rchild;
private int value ;
public BinaSortTree getLchild() {
return lchild;
}
public void setLchild(BinaSortTree lchild) {
this.lchild = lchild;
}
public BinaSortTree getRchild() {
return rchild;
}
public void setRchild(BinaSortTree rchild) {
this.rchild = rchild;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaSortTree(BinaSortTree lchild, BinaSortTree rchild, int value) {
super();
this.lchild = lchild;
this.rchild = rchild;
this.value = value;
}
public BinaSortTree(int value) {
this.value = value;
}
}
}
/**************************************************************
Problem: 1509
User: wangzhenqing
Language: Java
Result: Accepted
Time:2780 ms
Memory:70592 kb
****************************************************************/