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);         }}



原创粉丝点击