找到二叉树中的距离
难度:
标签:
题目描述
代码结果
运行时间: 48 ms, 内存: 20.4 MB
/*
* Leetcode 1740: Find Distance in a Binary Tree
*
* Problem Statement:
* Given the root of a binary tree and two integer values node1 and node2,
* return the distance between the nodes of value node1 and node2 in the tree.
* The distance between two nodes is the number of edges on the path from one to the other.
*
* Approach:
* 1. Find the lowest common ancestor (LCA) of the two nodes.
* 2. Calculate the distance from the LCA to each of the two nodes.
* 3. Sum these two distances to get the distance between the nodes.
*
* This solution utilizes Java Streams to perform the same operations.
*/
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public int findDistance(TreeNode root, int p, int q) {
TreeNode lca = findLCA(root, p, q);
int d1 = findLevel(lca, p, 0);
int d2 = findLevel(lca, q, 0);
return d1 + d2;
}
private TreeNode findLCA(TreeNode root, int p, int q) {
if (root == null || root.val == p || root.val == q) {
return root;
}
TreeNode left = findLCA(root.left, p, q);
TreeNode right = findLCA(root.right, p, q);
if (left != null && right != null) {
return root;
}
return Optional.ofNullable(left).orElse(right);
}
private int findLevel(TreeNode root, int val, int level) {
if (root == null) {
return -1;
}
if (root.val == val) {
return level;
}
return Optional.of(findLevel(root.left, val, level + 1))
.filter(l -> l != -1)
.orElse(findLevel(root.right, val, level + 1));
}
}
解释
方法:
该题解采用两个主要步骤来找到二叉树中两个节点p和q之间的距离。首先,使用最低公共祖先(LCA)算法找出p和q的共同祖先节点。然后,计算从这个LCA节点到p和q的距离,并将这两个距离相加得到最终的结果。LCA算法通过递归检查当前节点是否为p、q或者它们的共同祖先来工作。找到距离则使用深度优先搜索(DFS),从LCA节点开始向下寻找p和q,计算它们到LCA的距离。
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在LCA函数中,当遇到root为None或root的值等于p或q时直接返回root的设计原理是什么?
▷🦆
LCA算法在遇到左右子树均非空时选择返回root的理由是什么,这种情况下如何确定root确实是p和q的最低公共祖先?
▷🦆
在DFS函数中,当左右子树的DFS返回值都是-1时,为什么选择返回-1,这代表了什么情况?
▷🦆
DFS函数使用了max函数来计算返回值,为什么使用max而不是min,这与树的结构有什么关系?
▷