【Hot100】LeetCode—236. 二叉树的最近公共祖先



1- 思路

递归 + 自底向上

① 自底向上的逻辑的话

  • 需要采用后续遍历的方式,最后处理中间结点

② 递归

  • 2.1 参数和返回值
    • 返回值为 TreeNode,参数为 root==null || root == p || root == q

image.png

  • 2.3 遍历逻辑(后续遍历:左右中)

2- 实现

⭐236. 二叉树的最近公共祖先——题解思路

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
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;
        }else if (left!=null && right==null){
            return left;
        }else if(left==null && right!=null){
            return right;
        }else{
            return root;
        }

    }
}

3- ACM 实现

public class commonAncestor {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode build(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        String input = str.replace("[", "");
        input = input.replace("]", "");
        String[] parts = input.split(",");

        Integer[] nums = new Integer[parts.length];
        for (int i = 0; i < parts.length; i++) {
            if (!parts[i].equals("null")) {
                nums[i] = Integer.parseInt(parts[i]);
            } else {
                nums[i] = null;
            }
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while (!queue.isEmpty() && index < parts.length) {
            TreeNode node = queue.poll();
            if (index < nums.length && nums[index] != null) {
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if (index < nums.length && nums[index] != null) {
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }
    public static TreeNode findNode(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftResult = findNode(root.left, val);
        if (leftResult != null) {
            return leftResult;
        }
        return findNode(root.right, val);
    }
    public static 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;
        }else if (left!=null && right==null){
            return left;
        }else if(left==null && right!=null){
            return right;
        }else{
            return root;
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);
        // 读取p和q的值
        int pVal = sc.nextInt();
        int qVal = sc.nextInt();
        TreeNode p = findNode(root,pVal);
        TreeNode q = findNode(root,qVal);
        TreeNode res = lowestCommonAncestor(root,p,q);
        System.out.println("结果是"+res.val);
    }
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值