/** 298.Binary Tree longest consecutive Sequence * 2016-7-2 by Mingyang * Fully homegrown code, using bottom-up ideas, first from the bottom of the node network Go on, and set a global variable res is always updated * similar to the uniq trees topic, very good! * Time must be n, similar to post order principle, space is also n, because the recursion * The extra space comes from implicit stack space due to recursion. * For a skewed binary tree, the recursion could go and the nn levels deep. */ Public intRes=0; Public intlongestconsecutive (TreeNode root) {longesthelper (root); returnRes; } Public intlongesthelper (TreeNode root) {if(root==NULL) return0; intleft=Longesthelper (Root.left); intright=Longesthelper (root.right); intCurrent=1; if(root.left!=NULL&&root.val+1==root.left.val) { Current=left+1; } if(root.right!=NULL&&root.val+1==root.right.val) { Current=math.max (current,right+1); } Res=Math.max (res,current); returnCurrent ; }
298.Binary Tree longest consecutive Sequence