手撕真题-计算二叉树中两个节点之间的距离

思路

求解代码

java 复制代码
 public class TreeDistance {

        /**
         * 计算两个节点在二叉树中的距离
         *
         * @param root 二叉树的根节点
         * @param p    第一个节点
         * @param q    第二个节点
         * @return 两个节点之间的距离
         */
        public int distanceBetweenNodes(TreeNode root, TreeNode p, TreeNode q) {
            // 首先找到两个节点的最近公共祖先(LCA)
            TreeNode lca = findLCA(root, p, q);
            // 计算节点p到LCA的深度
            int depthP = findDepth(lca, p);
            // 计算节点q到LCA的深度
            int depthQ = findDepth(lca, q);
            // 两个节点之间的距离等于它们到LCA的深度之和
            return depthP + depthQ;
        }

        /**
         * 查找二叉树中两个节点的最近公共祖先(LCA)
         *
         * @param root 二叉树的根节点
         * @param p    第一个目标节点
         * @param q    第二个目标节点
         * @return 最近公共祖先节点,如果其中一个节点是根节点,则返回该节点;如果两个节点分别在左右子树中,则返回它们的根节点
         */
        private TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q) {
            // 如果根节点为空,或者根节点是p或q中的一个,直接返回根节点
            if (root == null || root == p || root == q) {
                return root;
            }
            // 递归查找左子树中的LCA
            TreeNode left = findLCA(root.left, p, q);
            // 递归查找右子树中的LCA
            TreeNode right = findLCA(root.right, p, q);
            // 如果左右子树都找到了非空节点,说明p和q分别在左右子树中,当前节点就是LCA
            if (left != null && right != null) {
                return root;
            }
            // 如果左子树找到非空节点,则返回左子树的结果,否则返回右子树的结果
            return left != null ? left : right;
        }


        /**
         * 查找目标节点在给定祖先节点下的深度
         *
         * @param ancestor 起始祖先节点
         * @param target   目标节点
         * @return 目标节点相对于祖先节点的深度,如果未找到则返回-1
         */
        private int findDepth(TreeNode ancestor, TreeNode target) {
            // 如果祖先节点为空,说明无法找到目标节点,返回-1
            if (ancestor == null) {
                return -1;
            }
            // 如果当前节点就是目标节点,深度为0
            if (ancestor == target) {
                return 0;
            }
            // 递归查找左子树
            int left = findDepth(ancestor.left, target);
            // 如果在左子树中找到目标节点,返回左子树深度+1
            if (left != -1) {
                return left + 1;
            }
            // 递归查找右子树
            int right = findDepth(ancestor.right, target);
            // 如果在右子树中找到目标节点,返回右子树深度+1,否则返回-1
            return right != -1 ? right + 1 : -1;
        }
    }
相关推荐
standovon20 分钟前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
Amumu1213826 分钟前
Js:正则表达式(二)
开发语言·javascript·正则表达式
Sgf2271 小时前
ES8(ES2017)新特性完整指南
开发语言·javascript·ecmascript
IAUTOMOBILE1 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi1 小时前
PostgreSQL版本选择
java
皮皮林5511 小时前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea
好大哥呀1 小时前
C++ Web 编程
开发语言·前端·c++
MX_93591 小时前
SpringMVC请求参数
java·后端·spring·servlet·apache
ID_180079054731 小时前
小红书笔记评论 API,Python 调用示例与完整 JSON 返回参考
java·开发语言
lifewange1 小时前
java连接Mysql数据库
java·数据库·mysql