• Leetcode 剑指Offer 68 -II 二叉树的最近公共祖先


    题目定义:

    给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
    
    百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,
    满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
    
    例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4]
    
    			  3
        		        /  
                          5     1
                         /    / 
                        6   2 0   8
                           / 
                          7   4
    
    示例 1:
    
    输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
    输出: 3
    解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
    示例 2:
    
    输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
    输出: 5
    解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
    
    
    说明:
    所有节点的值都是唯一的。
    p、q 为不同节点且均存在于给定的二叉树中。
    注意:本题与主站 236 题相同:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
    
    

    递归方式:

    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) return right;
            if(right == null) return left;
            return root;
        }
    }
    

    迭代方式:

    class Solution {
        private Map<TreeNode,TreeNode> path = new HashMap<>();
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(root == p || root == q)
                return root;
            dfs(root);
            Set<TreeNode> trail = getTrail(p);
            return getCommonAncestor(trail,q);
            
        }
        private void dfs(TreeNode root){
            if(root.left != null){
                path.put(root.left,root);
                dfs(root.left);
            }
            if(root.right != null){
                 path.put(root.right,root);
                dfs(root.right);
            }
        }
    
        private Set<TreeNode> getTrail(TreeNode node){
            Set<TreeNode> trail = new HashSet<>();
            while(node != null){
                trail.add(node);
                node = path.get(node);
            }
            return trail;
        }
        
        private TreeNode getCommonAncestor(Set<TreeNode> trail,TreeNode q){
            for(;;){
                if(trail.contains(q))
                    return q;
                q = path.get(q);
            }
        }
    }
    

    参考:

    https://leetcode-cn.com/problems/er-cha-shu-de-zui-jin-gong-gong-zu-xian-lcof/

    https://www.cnblogs.com/CodingXu-jie/p/14001945.html

  • 相关阅读:
    顺序栈的模拟
    Maven安装教程详解
    C# CRC
    Oracle知识分类之常见规范
    Oracle知识分类之异常
    el-tree树结构的数据封装
    sessionStorage和localStorage
    el-form 中为el-input 添加正则校验 以及el-input正则限制
    动态组件与父子传值的灵活应用 ref控制子组件 props,emit传值
    el-tree树组件的封装 (运用递归对接口返回当前菜单数据为对象处理)
  • 原文地址:https://www.cnblogs.com/CodingXu-jie/p/14154155.html
Copyright © 2020-2023  润新知