博客
关于我
剑指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/

    你可能感兴趣的文章
    pandas :如何删除以NaN为列名的多个列?
    查看>>
    pandas :我如何对堆叠的条形图进行分组?
    查看>>
    pandas :按移位分组和累加和(GroupBy Shift And Cumulative Sum)
    查看>>
    pandas :检测一个DF和另一个DF之间缺失的列
    查看>>
    Pandas-从具有嵌套列表列表的现有列创建动态列时出错
    查看>>
    Pandas-通过对列和索引的值求和来合并两个数据框
    查看>>
    pandas.columns、get_dummies等用法
    查看>>
    pandas.DataFrame.copy(deep=True) 实际上并不创建深拷贝
    查看>>
    pandas.read_csv()的详解-ChatGPT4o作答
    查看>>
    PANDAS.READ_EXCEL()输出‘;溢出错误:日期值超出范围‘;而不存在日期列
    查看>>
    pandas100个骚操作:再见 for 循环!速度提升315倍!
    查看>>
    Pandas:如何根据其他列值的条件对列进行求和?
    查看>>
    Pandas:对给定列求和 DataFrame 行
    查看>>
    Pandas、groupby 和特定月份的求和
    查看>>
    Pandas、Matplotlib、Pyecharts数据分析实践
    查看>>
    Pandas中文官档 ~ 基础用法1
    查看>>
    Pandas中文官档~基础用法2
    查看>>
    SpringBoot+Vue+OpenOffice实现文档管理(文档上传、下载、在线预览)
    查看>>
    Pandas中文官档~基础用法5
    查看>>
    Pandas中文官档~基础用法6
    查看>>