zoukankan      html  css  js  c++  java
  • 面试题68

    题目描述:

    测试用例:

    思路:

    • 显而易见这题使用递归算法找左右子树

    基本流程:

    1. 递归结束条件:

      1. 传入参数root为null,返回root

      2. 传入参数root为p或q节点,返回root

    2. 递归左右子树,获得递归结果left,right

    3. 递归结果的四种情况:

      1. 左右子树都不为null,返回根root就是最近公共祖先

      2. 左子树为null,返回右子树,由于左子树都不包含p或q一定不为公共祖先,右子树可能为公共祖先pqnull

      3. 同理:右子树为null,返回左子树。

      4. 补充:情况2和情况3包含了三种情况:

        1. 左子树为null,右子树不为null

        2. 右子树为null,左子树不为null

        3. 左右子树都为null

    代码:

    import java.util.Objects;
    
    public class P236LowestCommonAncestorOfABinaryTree {
    
        static class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
    
            TreeNode(int x) {
                val = x;
            }
        }
    
        //root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
        public static void main(String[] args) {
            Solution solution = new P236LowestCommonAncestorOfABinaryTree().new Solution();
            // TO TEST
            TreeNode root = new TreeNode(3);
            TreeNode root1 = new TreeNode(5);
            TreeNode root2 = new TreeNode(1);
            TreeNode root3 = new TreeNode(6);
            TreeNode root4 = new TreeNode(2);
            TreeNode root5 = new TreeNode(0);
            TreeNode root6 = new TreeNode(8);
            TreeNode root7 = new TreeNode(7);
            TreeNode root8 = new TreeNode(4);
    
    
            root.left = root1;
            root.right = root2;
            root1.left = root3;
            root1.right = root4;
            root2.left = root5;
            root2.right = root6;
            root4.left = root7;
            root4.right = root8;
    
            root3.left = root3.right = root5.left = root5.right = root6.left = root6.right = root7.left = root7.right = root8.left = root8.right = null;
    
            System.out.println(solution.lowestCommonAncestor(root, root1, root2).val);
        }
        //leetcode submit region begin(Prohibit modification and deletion)
    
        /**
         * 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 (Objects.isNull(root) || root == p || root == q)
                    return root;
    
                TreeNode left = lowestCommonAncestor(root.left, p, q);
                TreeNode right = lowestCommonAncestor(root.right, p, q);
    
                if (Objects.nonNull(left) && Objects.nonNull(right)) return root;
                else if (Objects.isNull(left)) return right;
                else return left;
            }
        }
    }
  • 相关阅读:
    求有序数组中不重复数字的出现次数
    ThreadLocal在Spring事务管理中的应用
    spring声明式的事务管理
    bootstrap-table 下一页点击失效
    IE浏览器下ajax和缓存的那些事儿
    vue学习第二步——目录结构
    vue学习第一步——自动化构建项目
    bootstrap-select 默认搜索
    webuploader的一些坑
    easyUI combobox 添加空白项
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12545160.html
Copyright © 2011-2022 走看看