package com.company;
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
* 美團在線編程題,最近公共祖先,使用後序遍歷做
*/
class Node {
Node lchild;
Node rchild;
int val;
Node(int val) {
this.val = val;
}
}
public class Main1 {
public static Node getAncestor(Node head, Node s, Node t) {
if (head == null || head == s || head == t) {
return null;
}
Node left = getAncestor(head.lchild, s, t);
Node right = getAncestor(head.rchild, s, t);
if (left != null && right != null) {
return head;
}
return left != null ? left : right;
}
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
while (in.hasNext()) {
}
}
}
最近公共祖先(美團在線編程題)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.