二叉树专题-求两个节点的最低公共祖先

两个节点的最低公共祖先

题目描述

给出二叉树中的两个节点,查找其最低的公共祖先。

递归解法

解题思路,递归时,就要把可能的情况列出来
1.和给出的头节点有关,左树找出一个节点,右树找出另外一个节点。
2.左树找出两个节点,
3.在右树找出两个节点
根据上面情况,我们定义每次递归需要的结果,是否找到a节点,是否找到b节点,是否找到最低公共祖先

定义一个二叉树

 public class Node{
        int val;
        Node left;
        Node right;

        public Node(int val) {
            this.val = val;
        }
    }

定义消息体

  public class Info{
        boolean findA;
        boolean findB;
        Node cur;

        public Info(boolean findA, boolean findB, Node cur) {
            this.findA = findA;
            this.findB = findB;
            this.cur = cur;
        }
    }

递归过程

   public static Info process(Node head,Node a,Node b){
        if (head == null){
            return new Info(false,false,null);
        }
        Info left = process(head.left,a,b);
        Info right = process(head.right,a,b);
        boolean findA = head == a || left.findA || right.findA;
        boolean findB = head == b || left.findB || right.findB;
        Node cur = null;
        if (left.cur != null){
            cur = left.cur;
        } else if (right.cur != null) {
            cur = right.cur;
        }else {
            if (findA && findB){
                cur = head;
            }
        }

        return new Info(findA,findB,cur);
    }


	//调用递归
	public static Node lowestAncestor(Node head, Node a, Node b) {
		return process(head, a, b).cur;
	}

常规解法

public static Node lowestAncestor1(Node head, Node o1, Node o2) {
		if (head == null) {
			return null;
		}
		// key的父节点是value
		HashMap<Node, Node> parentMap = new HashMap<>();
		parentMap.put(head, null);
		fillParentMap(head, parentMap);
		HashSet<Node> o1Set = new HashSet<>();
		Node cur = o1;
		o1Set.add(cur);
		while (parentMap.get(cur) != null) {
			cur = parentMap.get(cur);
			o1Set.add(cur);
		}
		cur = o2;
		while (!o1Set.contains(cur)) {
			cur = parentMap.get(cur);
		}
		return cur;
	}

	public static void fillParentMap(Node head, HashMap<Node, Node> parentMap) {
		if (head.left != null) {
			parentMap.put(head.left, head);
			fillParentMap(head.left, parentMap);
		}
		if (head.right != null) {
			parentMap.put(head.right, head);
			fillParentMap(head.right, parentMap);
		}
	}

二叉树其他经典题目
判断是不是搜索二叉树
求出二叉树的最大宽度

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值