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

目录

  • 题目
  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐236. 二叉树的最近公共祖先------题解思路](#⭐236. 二叉树的最近公共祖先——题解思路)
  • [3- ACM实现](#3- ACM实现)

题目


1- 思路

模式识别

  • 模式1:二叉树最近公共祖先 ------> 递归 + 判断

递归思路,分情况判断

  • 1.参数及返回值
    • public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
  • 2.递归终止条件
    • 遇到 null root == proot = q 的情况,此时递归结束。
  • 3.单层递归逻辑 4 种情况
    • 递归 left 赋值
    • 递归 right 赋值
    • ① 左右都为 null、此时返回 null
    • ② 左不 **null** 、右 **null** 返回左
    • ③ 左 **null**、右不 **null** 返回右
    • ④ 直接返回 root

2- 实现

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

java 复制代码
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实现

java 复制代码
public class lowestCommonAncestor {


    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int x){
            val = x;
        }
    }


    public static TreeNode build(Integer[] nums){
        TreeNode root = new TreeNode(nums[0]);
        Queue<TreeNode> queue =new LinkedList<>();
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty() && index < nums.length){
            TreeNode node = queue.poll();

            if(nums[index]!=null && index<nums.length){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(nums[index]!=null && index<nums.length){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }


    public static TreeNode findOne(TreeNode root,int val){
        if(root==null) return root;
        if(root.val == val) return root;

        TreeNode left = findOne(root.left,val);
        if(left!=null) return left;
        return findOne(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);
        System.out.println("输入二叉树构造数组");
        String input = sc.nextLine();
        input = input.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;
            }
        }

        TreeNode root = build(nums);
        System.out.println("输入val1");
        TreeNode p = findOne(root,sc.nextInt());

        System.out.println("输入val2");
        TreeNode q = findOne(root,sc.nextInt());

        TreeNode res = lowestCommonAncestor(root,p,q);
        System.out.println("公共父节点值为"+res.val);
    }
}

相关推荐
Mephisto.java23 分钟前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli24 分钟前
滑动窗口->dd爱框框
算法
丶Darling.25 分钟前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo52036 分钟前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
jiyisuifeng19911 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂1 小时前
实验4 循环结构
c语言·算法·基础题
新晓·故知1 小时前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表
总裁余(余登武)2 小时前
算法竞赛(Python)-万变中的不变“随机算法”
开发语言·python·算法
Eric.Lee20212 小时前
音频文件重采样 - python 实现
人工智能·python·深度学习·算法·audio·音频重采样
一个不知名程序员www2 小时前
leetcode第189题:轮转数组(C语言版)
c语言·leetcode