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

    你可能感兴趣的文章
    oracle数据库笔记---pl/sql的基础使用方法
    查看>>
    Transformer 架构解释
    查看>>
    Oracle数据库表空间 数据文件 用户 以及表创建的SQL代码
    查看>>
    oracle数据库零碎---Oracle Merge 使用,表中存在数据就修改,没有数据自动添加
    查看>>
    Oracle数据库验证IMP导入元数据是否会覆盖历史表数据
    查看>>
    oracle数据插入表,oracle同时向多表插入数据
    查看>>
    oracle数据类型和对应的java类型
    查看>>
    【C++进阶篇】——string类的使用
    查看>>
    Oracle未开启审计情况下追踪表变更记录
    查看>>
    Oracle条件查询
    查看>>
    Oracle查看数据库会话连接
    查看>>
    Oracle查询前几条数据的方法
    查看>>
    oracle树形查询 start with connect by
    查看>>
    oracle毕业论文题目,历届毕业论文申报题目大全.doc
    查看>>
    oracle求助---win7下oracle配置相关疑问Starting Oracle Enterprise Manager 10g Database Control ...发生系统错误 5。
    查看>>
    Oracle流程控制语句
    查看>>
    oracle深度解析检查点
    查看>>
    Oracle游标
    查看>>
    oracle游标数最大数,Oracle 最大连接数 最大游标数
    查看>>
    oracle用户改名
    查看>>