题目:
给定一个二叉树,找到该树中两个指定节点的最近公共祖先
最近公共祖先的定义为:对于有根树T的两个节点p、q,最近公共祖先表示为一个节点x,满足x是p、q的祖先且x的深度尽可能大(一个节点也可以是它自己的祖先)
int child1 = 9;int child2 = 8;public TreeNode<Integer> getPublicParent(TreeNode<Integer> root) {if (root == null) {return null;}if (root.getData() == child1 || root.getData() == child2) {return root;}TreeNode publicParentLeft = getPublicParent(root.lTreeNode);TreeNode publicParentRight = getPublicParent(root.rTreeNode);if (publicParentLeft != null && publicParentRight != null) {return root;} else if (publicParentLeft != null) {return publicParentLeft;} else if (publicParentRight != null) {return publicParentRight;}return null;}
