|
1 | 1 | package 数据结构.树.递归.节点;
|
2 | 2 |
|
| 3 | +import 数据结构.树.TreeNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | + |
3 | 7 | /**
|
4 | 8 | * Created by 周杰伦 on 2018/4/20.
|
5 | 9 | */
|
6 | 10 | public class 二叉树的最近公共祖先 {
|
| 11 | + // public static void main(String[] args) { |
| 12 | +//// TreeNode t0 = new TreeNode(3); |
| 13 | +//// TreeNode t1 = new TreeNode(5); |
| 14 | +//// TreeNode t3 = new TreeNode(6); |
| 15 | +//// TreeNode t4 = new TreeNode(2); |
| 16 | +//// TreeNode t5 = new TreeNode(7); |
| 17 | +//// TreeNode t6 = new TreeNode(4); |
| 18 | +//// |
| 19 | +//// t4.left = t5; |
| 20 | +//// t4.right = t6; |
| 21 | +//// t1.left = t3; |
| 22 | +//// t1.right = t4; |
| 23 | +//// t0.left = t1; |
| 24 | +//// |
| 25 | +//// |
| 26 | +//// TreeNode t7 = new TreeNode(1); |
| 27 | +//// TreeNode t8 = new TreeNode(0); |
| 28 | +//// TreeNode t9 = new TreeNode(8); |
| 29 | +//// |
| 30 | +//// t7.left = t8; |
| 31 | +//// t7.right = t9; |
| 32 | +//// t0.right = t7; |
| 33 | +//// |
| 34 | +//// System.out.println(lowestCommonAncestor(t0, t9, t8)); |
| 35 | +// TreeNode t1 = new TreeNode(1); |
| 36 | +// TreeNode t2 = new TreeNode(2); |
| 37 | +// TreeNode t3 = new TreeNode(3); |
| 38 | +// t1.left = t2; |
| 39 | +// t1.right = t3; |
| 40 | +// System.out.println(lowestCommonAncestor(t1, t2, t3)); |
| 41 | +// |
| 42 | +// } |
| 43 | + |
| 44 | + |
| 45 | + //复杂度是n + n + n*n*2 = n2; |
| 46 | + |
| 47 | + //最优解 |
| 48 | +// public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 49 | +// if (root == null || root == p || root == q) return root; |
| 50 | +// TreeNode left = lowestCommonAncestor(root.left, p, q); |
| 51 | +// TreeNode right = lowestCommonAncestor(root.right, p, q); |
| 52 | +// return left == null ? right : right == null ? left : root; |
| 53 | +// } |
| 54 | + |
| 55 | + |
| 56 | + //我的解法 |
| 57 | + //先打印中序遍历 |
| 58 | + //只保留两个节点中间的节点。 |
| 59 | + //计算距离的最小值,值最小的是最近的。 |
| 60 | + public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 61 | + if (root == null)return null; |
| 62 | + if (p == q)return p; |
| 63 | + if (p == root || q == root)return root; |
| 64 | + if (isParent(p,q))return p; |
| 65 | + if (isParent(q,p))return q; |
| 66 | + ArrayList<TreeNode> list = new ArrayList<>(); |
| 67 | + inorder(root, list); |
| 68 | + int i = -1,j = -1; |
| 69 | + for (TreeNode t : list) { |
| 70 | + if (t == p)i = list.indexOf(p); |
| 71 | + if (t == q)j = list.indexOf(q); |
| 72 | + if (i != -1 && j != -1)break; |
| 73 | + } |
| 74 | + int left = i > j ? j : i; |
| 75 | + int right = i > j ? i : j; |
| 76 | + for (int n = right;n < list.size(); n ++) { |
| 77 | + list.remove(n); |
| 78 | + } |
| 79 | + for (int m = 0;m <= left;m ++) { |
| 80 | + list.remove(0); |
| 81 | + } |
| 82 | + TreeNode minNode = root; |
| 83 | + int min = Integer.MAX_VALUE; |
| 84 | + if (list.size() == 1)return list.get(0); |
| 85 | + for (TreeNode t : list) { |
| 86 | + if (isParent(t, p) && isParent(t, q)) { |
| 87 | + int cnt = cnt(t, p, 1) + cnt(t, q, 1); |
| 88 | + if (cnt < min) { |
| 89 | + min = cnt; |
| 90 | + minNode = t; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return minNode; |
| 95 | + } |
| 96 | + public static void inorder(TreeNode root, ArrayList<TreeNode> list) { |
| 97 | + if (root == null)return; |
| 98 | + inorder(root.left, list); |
| 99 | + list.add(root); |
| 100 | + inorder(root.right, list); |
| 101 | + return; |
| 102 | + } |
| 103 | + public static int cnt (TreeNode root, TreeNode p, int cnt) { |
| 104 | + if (root == null)return 0; |
| 105 | + if (root == p)return cnt; |
| 106 | + int left = cnt(root.left, p, cnt + 1); |
| 107 | + int right = cnt(root.right, p, cnt +1); |
| 108 | + return left != 0 ? left : right; |
| 109 | + } |
| 110 | + public static boolean isParent(TreeNode t, TreeNode p) { |
| 111 | + if (t == p) return true; |
| 112 | + if (t == null) return false; |
| 113 | + return isParent(t.left, p) || isParent(t.right, p); |
| 114 | + } |
| 115 | +// |
| 116 | +// public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 117 | +// if (root == null) return null; |
| 118 | +// if (isParent(root, q)) return p; |
| 119 | +// if (isParent(q, p)) return q; |
| 120 | +// return inorder(root, p, q, root); |
| 121 | +// } |
| 122 | +// |
| 123 | +// public TreeNode inorder(TreeNode root, TreeNode p, TreeNode q, TreeNode pre) { |
| 124 | +// if (root == null) return pre; |
| 125 | +// if (isParent(root, p) && isParent(root, q)) { |
| 126 | +// TreeNode left = inorder(root.left, p, q, root); |
| 127 | +// TreeNode right = inorder(root.right, p, q, root); |
| 128 | +// if (left != null) return left; |
| 129 | +// if (right != null) return right; |
| 130 | +// } |
| 131 | +// return null; |
| 132 | +// } |
| 133 | + |
7 | 134 | }
|
| 135 | + |
0 commit comments