Lowest Common Ancestor of a Binary Tree
来源:互联网 发布:单片机检测电压原理 编辑:程序博客网 时间:2024/06/02 04:36
题目描述:
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 allowa 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); } }}
之前findPath我是这么写的,这么做会超时,开销也太大。
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; }
0 0
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree(***)
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- android studio for android learning (六)应用程序窗体显示状态操作(requestWindowFeature等的应用)
- 感知器算法(perceptron algorithm)
- ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- HashTable和HashMap的区别详解
- ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
- Lowest Common Ancestor of a Binary Tree
- python爬虫的使用
- 初探tcp和udp
- 在Linux下用C语言写一个小程序实现一个进度条编写Makefile来进行运行
- c++第六次作业
- 【IDEA】无法创建Maven项目
- Hibernate-投影查询
- 互联网领域常见的数据存储方式
- Kubernetes在Hazelcast平台上的原生云部署(概述)