题目:
给定二叉树头结点head,任何两个节点之间都有距离,求整棵二叉树最大距离。
二叉树如下图所示,假设从x到b,中间节点只能走一次,我们人为规定距离就是整条路径的节点数量,所以距离是3:x -> e -> b。这棵树最大的距离是5(x - > c)。
分析:
所以,一棵树最大的距离共有2类3种情况。一种是经过的头节点,一种是没经过头节点,
- 根据题目分析构建Info信息所需要的变量有哪些
1:如上图所示:经过了头结点a,它的最大距离就是x -> c 就是相当于左子树高度 + 右子树高度 + 1、
2:如果没经过头结点,如下图所示:那它的最大长度就是左子树的最大距离(12 -> 8) (也可以将左子树中的1节点看做是一颗二叉树的head节点) - 根据上面两条可以分析出构建Info对象需要树的高度 height和树的最大距离maxDistance
递归方式
public static class Node{
int val;
Node left;
Node right;
public Node(int val){
this.val = val;
}
}
//收集左右子树信息,树高度和最大距离
public static class Info{
int height;
int maxDistinct;
public Info(int height,int maxDistinct){
this.height = height;
this.maxDistinct = maxDistinct;
}
}
public static int maxDistinct(Node head){
if (head == null ){
return 0;
}
return process(head).maxDistinct;
}
public static Info process(Node head){
if (head == null){
return new Info(0,0);
}
Info leftInfo = process(head.left);
Info rightInfo = process(head.right);
int height = Math.max(leftInfo.height,rightInfo.height) + 1;
//一共3种情况
//最大距离是左子树的最大距离
int p1 = leftInfo.maxDistinct;
//最大距离是右子树的最大距离
int p2 = rightInfo.maxDistinct;
//经过了head节点,那么就是左右子树的高度相加 再加上head节点的 1
int p3 = leftInfo.height + rightInfo.height + 1;
//在3种情况中求得这棵树的最大距离
int maxDistinct = Math.max(p3,Math.max(p1,p2));
return new Info(height,maxDistinct);
}
暴力方式
遍历二叉树,填充到list中和map中(key:当前节点 value:当前节点父节点)。
两层循环遍历list,让当前节点和每个节点作比较计算距离。
先找到两个节点的最小公共祖先。从o1,o2分别向最小公共祖先遍历,没循环一次distance++;
将o1,o2的distance相加 - 1就是当前最大距离。
代码
public static int maxDistance2(Node head) {
if (head == null) {
return 0;
}
List<Node> list = getPrelist(head);
HashMap<Node, Node> parentMap = getParentMap(head);
int max = 0;
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
max = Math.max(max, distance(parentMap, list.get(i), list.get(j)));
}
}
return max;
}
private static int distance(HashMap<Node, Node> map, Node o1, Node o2) {
HashSet<Node> parentSet = new HashSet<>();
Node cur = o1;
parentSet.add(o1);
//将所有父节点添加到set中
while (map.get(cur) != null) {
cur = map.get(cur);
parentSet.add(cur);
}
cur = o2;
//找到o1,o2最小公共祖先
while (!parentSet.contains(cur)) {
cur = map.get(cur);
}
Node lowestAncestor = cur;
cur = o1;
int distance1 = 1;
while (cur != lowestAncestor) {
distance1++;
cur = map.get(cur);
}
int distance2 = 1;
cur = o2;
while (cur != lowestAncestor) {
distance2++;
cur = map.get(cur);
}
return distance1 + distance2 - 1;
}
private static HashMap<Node, Node> getParentMap(Node head) {
HashMap<Node, Node> map = new HashMap<>();
map.put(head, null);
fillMap(head, map);
return map;
}
private static void fillMap(Node head, HashMap<Node, Node> map) {
if (head == null) {
return;
}
if (head.left != null) {
map.put(head.left, head);
fillMap(head.left, map);
}
if (head.right != null) {
map.put(head.right, head);
fillMap(head.right, map);
}
}
private static List<Node> getPrelist(Node head) {
List<Node> list = new ArrayList<>();
fillList(head, list);
return list;
}
private static void fillList(Node head, List<Node> list) {
if (head == null) {
return;
}
list.add(head);
fillList(head.left, list);
fillList(head.right, list);
}
测试
// for test
public static Node generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue);
}
// for test
public static Node generate(int level, int maxLevel, int maxValue) {
if (level > maxLevel || Math.random() < 0.5) {
return null;
}
Node head = new Node((int) (Math.random() * maxValue));
head.left = generate(level + 1, maxLevel, maxValue);
head.right = generate(level + 1, maxLevel, maxValue);
return head;
}
public static void main(String[] args) {
int maxLevel = 4;
int maxValue = 100;
int testTimes = 1000;
for (int i = 0; i < testTimes; i++) {
Node head = generateRandomBST(maxLevel, maxValue);
if ( maxDistance1(head) != maxDistance2(head)) {
System.out.println("Oops!");
break;
}
}
System.out.println("finish!");
}