298.Binary Tree longest consecutive Sequence

Source: Internet
Author: User

    /** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.