博客
关于我
剑指offer--树--树中两个结点的最低公共祖先
阅读量:135 次
发布时间:2019-02-26

本文共 2510 字,大约阅读时间需要 8 分钟。

???????????????????????????????

  • ??????????????????????????????????????DFS???????
  • ??????????????????????????????????????????????
  • ??????????????

    import java.util.ArrayList;import java.util.Stack;public class Test2 {    public static void main(String[] args) {        // ????        TreeNode root = new TreeNode(6);        TreeNode left = new TreeNode(2);        left.left = new TreeNode(0);        left.right = new TreeNode(4);        left.right.left = new TreeNode(7);        left.right.right = new TreeNode(9);        root.left = left;        TreeNode right = new TreeNode(8);        right.left = new TreeNode(3);        right.left.right = new TreeNode(5);        root.right = right;        System.out.println(lowestCommonAncestorII(root, left, right));    }    public static TreeNode lowestCommonAncestorII(TreeNode root, TreeNode p, TreeNode q) {        List
    pPath = findPath(root, p); List
    qPath = findPath(root, q); // ??????????null if (pPath == null || qPath == null) { return null; } // ??????????????? int minLength = Math.min(pPath.size(), qPath.size()); TreeNode common = null; for (int i = 0; i < minLength; i++) { if (pPath.get(i) == qPath.get(i)) { common = pPath.get(i); } else { break; } } return common; } private static List
    findPath(TreeNode root, TreeNode target) { List
    path = new ArrayList<>(); Stack
    stack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { TreeNode node = stack.pop(); if (node == target) { // ????????????path? while (!stack.isEmpty()) { path.add(stack.pop()); } return path; } if (node.left != null) { stack.push(node.left); } if (node.right != null) { stack.push(node.right); } } return null; }}

    ????

  • findPath ???

    • ?????????????DFS?????????????????
    • ???????????????????????????????????
  • lowestCommonAncestorII ???

    • ?? findPath ??????????p?q????
    • ?????????????????????
    • ?????????????????????????????????????
  • ??????

    • main ???????????? lowestCommonAncestorII ???????????
    • lowestCommonAncestorII ?????????????????????????????
    • findPath ????????DFS?????????

    ????????????O(n)???????????????????????????????

    转载地址:http://tnzu.baihongyu.com/

    你可能感兴趣的文章
    OAuth 及 移动端鉴权调研
    查看>>
    OAuth2 + Gateway统一认证一步步实现(公司项目能直接使用),密码模式&授权码模式
    查看>>
    OAuth2 Provider 项目常见问题解决方案
    查看>>
    OAuth2 vs JWT,到底怎么选?
    查看>>
    Vue.js 学习总结(14)—— Vue3 为什么推荐使用 ref 而不是 reactive
    查看>>
    oauth2-shiro 添加 redis 实现版本
    查看>>
    OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
    查看>>
    OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
    查看>>
    OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
    查看>>
    OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
    查看>>
    OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
    查看>>
    OAuth2.0_授权服务配置_三项内容_Spring Security OAuth2.0认证授权---springcloud工作笔记141
    查看>>
    OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
    查看>>
    OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
    查看>>
    OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
    查看>>
    OAuth2.0_授权服务配置_授权码模式_Spring Security OAuth2.0认证授权---springcloud工作笔记144
    查看>>
    OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
    查看>>
    OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
    查看>>
    OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
    查看>>
    oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
    查看>>