题目描述:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4
For example, the lowest common ancestor (LCA) of nodes 5
and 1
is 3
. Another example is LCA of nodes 5
and 4
is 5
, since a node can be a descendant of itself according to the LCA definition.
这就是找到从root到p和q的路径,找到他们最后一个相同的节点就是他们的父节点。
代码如下:
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { List<TreeNode> list1=new ArrayList<TreeNode>(); List<TreeNode> list2=new ArrayList<TreeNode>(); List<TreeNode> path1=new ArrayList<TreeNode>(); List<TreeNode> path2=new ArrayList<TreeNode>(); findPath(root, p, list1, path1); findPath(root, q, list2, path2); int i=0; for(;i<path1.size()&&i<path2.size();i++){ if(path1.get(i)!=path2.get(i)) break; } return path1.get(i-1); } public void findPath(TreeNode root, TreeNode p ,List<TreeNode> list ,List<TreeNode> result){ if(root==p){ list.add(root); result.addAll(list); return; } if(root.left!=null){ list.add(root); findPath(root.left, p, list, result); list.remove(list.size()-1); } if(root.right!=null){ list.add(root); findPath(root.right, p, list, result); list.remove(list.size()-1); } } }
public void findPath(TreeNode root, TreeNode p ,List<TreeNode> list ,List<TreeNode> result){ if(root==p){ list.add(root); result.addAll(list); return; } if(root.left!=null){ List<TreeNode> newlist1=new ArrayList<TreeNode>(list); list.add(root); findPath(root.left, p, newlist1, result); } if(root.right!=null){ List<TreeNode> newlist2=new ArrayList<TreeNode>(list); newlist2.add(root); findPath(root.right, p, newlist2, result); } }
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root==null || p==null || q==null) return null; List<TreeNode> pathp = new ArrayList<>(); List<TreeNode> pathq = new ArrayList<>(); pathp.add(root); pathq.add(root); getPath(root, p, pathp); getPath(root, q, pathq); TreeNode lca = null; for(int i=0; i<pathp.size() && i<pathq.size(); i++) { if(pathp.get(i) == pathq.get(i)) lca = pathp.get(i); else break; } return lca; } private boolean getPath(TreeNode root, TreeNode n, List<TreeNode> path) { if(root==n) { return true; } if(root.left!=null) { path.add(root.left); if(getPath(root.left, n, path)) return true; path.remove(path.size()-1); } if(root.right!=null) { path.add(root.right); if(getPath(root.right, n, path)) return true; path.remove(path.size()-1); } return false; }