lowest node's LCA
来源:互联网 发布:mac 终端连接数据库 编辑:程序博客网 时间:2024/06/11 20:31
给一个 二叉树/多叉树 , 求最深节点的最小公共父节点。
这题算是对LCA的一个扩展,由给定两个点换成了最深两个点,由二叉树换成多树。
难点1. 我们要返回什么?
不难看出来, 最后要返回一个node,所以TreeNode 肯定是需要返回的变量之一;另外一个是最长路径或者说最大深度。所以说,每个node往上层返的时候都需要返回这2个变量,一个是当前以node为root的时候最深common节点,一个是深度deep。为什么说这是难点之一?大部分leetcode的树的题,基本上都是只需要返回一个变量的,这时候要返回两个变量。于是需要创建一个class,这个类里面包含一个node跟一个int类型的MAXdeep变量。 所以说当对任意一个节点调用这个函数的时候,都将返回,commonAncestorOfDeepest的节点,以及这个节点所在路径的depth。
难点2. 多叉树,而不是二叉树。
或许你觉得多叉数,本质上与二叉树没有区别,不就是多了几个下一级的指针嘛?二叉树只有left, right。 多叉树则有N个,仅此而已。可是就因为需要用一个list来保存多叉树的指针,又会影响递归函数内的判断逻辑的复杂程度。说的更加直白一点,二叉树,你只需要比较一下left跟right就好了,而多叉树你需要写loop,以及count来遍历,所有的指针。
private static class Solution {-google 1point3acres private class ReturnVal { public int depth; //The depth of the deepest leaves on the current subtree public TreeNode lca;//The lca of the deepest leaves on the current subtree public ReturnVal(int d, TreeNode n) { depth = d; lca = n; } } public TreeNode LowestCommonAncestorOfDeepestLeaves(TreeNode root) { ReturnVal res = find(root, 0); return res.lca; } private ReturnVal find(TreeNode root, int depth) { if(root == null) { return new ReturnVal(-1, null); } else { ReturnVal lRes = find(root.left, depth+1); ReturnVal rRes = find(root.right, depth+1); if(lRes.depth == rRes.depth) { return new ReturnVal(lRes.depth==-1?depth:lRes.depth, root); } else { return new ReturnVal(Math.max(rRes.depth, lRes.depth), rRes.depth>lRes.depth?rRes.lca:lRes.lca); } } } }
class TreeNode{ int val; ArrayList<TreeNode> children; public TreeNode(int val){ this.val = val; children = new ArrayList<>(); }} class Result{ TreeNode node; int maxDepth; public Result(TreeNode node, int maxDepth){ this.node = node; this.maxDepth = maxDepth; }} public class LowestCommonAncestorForAnyTree{ public static TreeNode find(TreeNode root){ if(root == null || root.children.isEmpty()) return root; return helper(root).node; } public static Result helper(TreeNode root){ if(root.children.isEmpty()) return new Result(root, 1); int size = root.children.size(); int maxDepth = Integer.MIN_VALUE; Result r = new Result(root, maxDepth); for(int i = 0; i < size; i++){ Result tmp = helper(root.children.get(i)); if(tmp.maxDepth > maxDepth){ maxDepth = tmp.maxDepth; r.node = tmp.node; r.maxDepth = tmp.maxDepth + 1; } //Find multiple nodes which all are deepest leaf nodes else if(tmp.maxDepth == maxDepth){ r.node = root; } } return r; } public static void main(String[] args){ TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2);. TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); TreeNode n7 = new TreeNode(7); TreeNode n8 = new TreeNode(8); TreeNode n9 = new TreeNode(9); TreeNode n10 = new TreeNode(10); n1.children.add(n2); n1.children.add(n3); n1.children.add(n4); n2.children.add(n5); n2.children.add(n6); n4.children.add(n7); n5.children.add(n8); n5.children.add(n9); n6.children.add(n10); TreeNode res = find(n1); System.out.println(res.val); }}
阅读全文
0 0
- lowest node's LCA
- Lowest Common Ancestor(LCA)
- SPOJ LCA Lowest Common Ancestor
- LCA(lowest common ancestor)问题
- 最近公共祖先 LCA (Lowest Common Ancestor)
- LCA(lowest common ancestor)问题
- 剑指offer4-(LCA)Lowest Common Ancestor
- AIZU Lowest Common Ancestor Tarjan求LCA
- Range Minimum Query(RMQ) and Lowest Common Ancestor(LCA)
- 【LCA】最近公共祖先问题Lowest Common Ancestors
- Lowest Common Ancestor (LCA) of two nodes in graph
- LeetCode 236 Lowest Common Ancestor of a Binary Tree (LCA)
- 最近公共祖先LCA(Lowest Common Ancestor)
- HDU6162 Ch’s gift【LCA】
- hdu6162 Ch’s gift(LCA)
- HDU 6162 Ch’s gift(LCA)
- 什么是Node?(What's node) (译)
- Range Minimum Query and Lowest Common Ancestor(RMQ和LCA 找个时间翻译下)
- Codeforces Round 23
- sofia-sip下载地址
- 记忆化搜索 哈工大1865
- 进程程序替换
- 非常好的理解遗传算法的例子
- lowest node's LCA
- 一些有用的网站:
- 免费文献下载
- (总结)Nginx与Apache、Tomcat、Resin动静分离核心配置
- bootstrap-paginator分页-前后台用法示例
- UVALive2288 POJ1050 HDU1081 ZOJ1074 To The Max【最大子段和+DP】
- D3
- 使用分层实现业务处理
- Python语言常见异常错误类型详解