|
| 1 | +package pp.arithmetic.medium; |
| 2 | + |
| 3 | +import pp.arithmetic.Util; |
| 4 | +import pp.arithmetic.model.TreeNode; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by wangpeng on 2018/9/14. |
| 11 | + * 236.二叉树的最近公共祖先 |
| 12 | + * 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 |
| 13 | + * <p> |
| 14 | + * 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” |
| 15 | + * <p> |
| 16 | + * 例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4] |
| 17 | + * <p> |
| 18 | + * _______3______ |
| 19 | + * / \ |
| 20 | + * ___5__ ___1__ |
| 21 | + * / \ / \ |
| 22 | + * 6 _2_ 0 8 |
| 23 | + * / \ |
| 24 | + * 7 4 |
| 25 | + * 示例 1: |
| 26 | + * <p> |
| 27 | + * 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
| 28 | + * 输出: 3 |
| 29 | + * 解释: 节点 5 和节点 1 的最近公共祖先是节点 3。 |
| 30 | + * 示例 2: |
| 31 | + * <p> |
| 32 | + * 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
| 33 | + * 输出: 5 |
| 34 | + * 解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。 |
| 35 | + * 说明: |
| 36 | + * <p> |
| 37 | + * 所有节点的值都是唯一的。 |
| 38 | + * p、q 为不同节点且均存在于给定的二叉树中。 |
| 39 | + * |
| 40 | + * @see <a href="https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/">lowest-common-ancestor-of-a-binary-tree</a> |
| 41 | + */ |
| 42 | +public class _236_lowestCommonAncestor { |
| 43 | + public static void main(String[] args) { |
| 44 | + TreeNode treeNode = Util.generateTreeNode(); |
| 45 | + Util.printTree(treeNode); |
| 46 | + TreeNode treeNode1 = lowestCommonAncestor(treeNode, treeNode.left.left, treeNode.left); |
| 47 | + System.out.println(treeNode1.val); |
| 48 | + //更优 |
| 49 | + TreeNode treeNode2 = lowestCommonAncestor2(treeNode, treeNode.left.left, treeNode.left); |
| 50 | + System.out.println(treeNode2.val); |
| 51 | + } |
| 52 | + |
| 53 | + private static boolean isFinish = false; |
| 54 | + |
| 55 | + /** |
| 56 | + * 时间复杂度:O(n),不过这3*O(n)比方案2要复杂 |
| 57 | + * |
| 58 | + * @param root |
| 59 | + * @param p |
| 60 | + * @param q |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 64 | + List<TreeNode> qList = new ArrayList<>(); |
| 65 | + List<TreeNode> pList = new ArrayList<>(); |
| 66 | + isFinish = false; |
| 67 | + dfs(root, p, pList); |
| 68 | + isFinish = false; |
| 69 | + dfs(root, q, qList); |
| 70 | + TreeNode result = null; |
| 71 | + for (int i = 0; i < qList.size() && i < pList.size(); i++) { |
| 72 | + if (pList.get(i) != qList.get(i)) { |
| 73 | + break; |
| 74 | + } |
| 75 | + result = pList.get(i); |
| 76 | + } |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + private static void dfs(TreeNode root, |
| 81 | + TreeNode target, |
| 82 | + List<TreeNode> path) { |
| 83 | + if (root == null || isFinish) { |
| 84 | + return; |
| 85 | + } |
| 86 | + if (root == target) { |
| 87 | + isFinish = true; |
| 88 | + } |
| 89 | + path.add(root); |
| 90 | + dfs(root.left, target, path); |
| 91 | + dfs(root.right, target, path); |
| 92 | + if (!isFinish) { |
| 93 | + path.remove(path.size() - 1); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 采用的是后序遍历。 |
| 99 | + * 如果需要找的两个节点分别在我的左,右,那么我就是最近的父节点 |
| 100 | + * 如果只在我的left,那么left就是最近父节点,反之则是在right |
| 101 | + * 时间复杂度:O(n) |
| 102 | + * |
| 103 | + * @param root |
| 104 | + * @param p |
| 105 | + * @param q |
| 106 | + * @return |
| 107 | + */ |
| 108 | + public static TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) { |
| 109 | + if (root == null || root == p || root == q) { |
| 110 | + return root; |
| 111 | + } |
| 112 | + TreeNode leftNode = lowestCommonAncestor2(root.left, p, q); |
| 113 | + TreeNode rightNode = lowestCommonAncestor2(root.right, p, q); |
| 114 | + if (leftNode != null && rightNode != null) { |
| 115 | + return root; |
| 116 | + } |
| 117 | + return leftNode == null ? rightNode : leftNode; |
| 118 | + } |
| 119 | +} |
0 commit comments