[Leedcode][JAVA][第236题][二叉树的公共祖先][后序遍历][BFS]

239 篇文章 1 订阅
【问题描述】
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4]

输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3。

在这里插入图片描述

【解答思路】
1. DFS 后序遍历

在这里插入图片描述
在这里插入图片描述
时间复杂度:O(N^2) 空间复杂度:O(N)

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null && right == null) return null; // 1. 可省略
        if(left == null) return right; // 3.
        if(right == null) return left; // 4.
        return root; // 2. if(left != null and right != null)
    }
}


2. 路径法

在这里插入图片描述
在这里插入图片描述

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    Stack<TreeNode> stack = new Stack<>();
    HashMap<TreeNode, TreeNode> parent = new HashMap<>();
    //初始化根节点
    stack.push(root);
    parent.put(root, null);
    //将遍历过程中每个节点的父节点保存起来
    while (!parent.containsKey(p) || !parent.containsKey(q)) {
        TreeNode cur = stack.pop();
        if (cur.left != null) {
            stack.push(cur.left);
            parent.put(cur.left, cur);
        }
        if (cur.right != null) {
            stack.push(cur.right);
            parent.put(cur.right, cur);
        }
    }
    HashSet<TreeNode> path = new HashSet<>();
    // 倒着还原 p 的路径,并将每个节点加入到 set 中
    while (p != null) {
        path.add(p);
        p = parent.get(p);
    }

    // 倒着遍历 q 的路径,判断是否在 p 的路径中
    while (q != null) {
        if (path.contains(q)) {
            break;
        }
        q = parent.get(q);
    }
    return q;
}




【总结】
1.二叉树遍历
  • 前序遍历 先输出当前结点的数据,再依次遍历输出左结点和右结点
  • 中序遍历 先遍历输出左结点,再输出当前结点的数据,再遍历输出右结点
  • 后续遍历 先遍历输出左结点,再遍历输出右结点,最后输出当前结点的数据
2. 树的问题DFS BFS 递归思想 剪枝优化
3.堆栈+HashMap(记录父节点)+HashSet(记录路径)

转载链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/236-er-cha-shu-de-zui-jin-gong-gong-zu-xian-hou-xu/
转载链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-5-2/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值