Linked list focus problem, a lot of polygon, consider intersect disjoint, ring without ring + Find Leaves of Binary Tree (Java)

Source: Internet
Author: User

Break the loop at the last node which pointed to the entry.

Given a binary tree, collect a tree ' s nodes as if you were doing this:collect and remove all leaves, repeat until the Tre E is empty.

Example:
Given binary Tree

          1         /         2   3       /\           4   5    

Returns [4, 5, 3], [2], [1].

ollowup: Each node still has a left,right pointer, but the structure is not a tree, but graph how to calculate, consider there is/no circular dependency two kinds of situations;the method is to record the calculated node level and a set record with HashMap dependent node
Public list<list<integer>> findleaves (TreeNode root) {    list<list<integer>> result = new Arraylist<list<integer>> ();    Helper (result, root);    return result;} Traverse the tree bottom-up recursivelyprivate int helper (list<list<integer>> List, TreeNode root) {    if (root==null)        return-1;     int left = helper (list, root.left);    int right = Helper (list, root.right);    int curr = Math.max (left, right) +1;     The first time this code is reached was when curr==0,    //since The tree was bottom-up processed.    if (List.size () <=curr) {        list.add (new arraylist<integer> ());    }     List.get (Curr). Add (root.val);     return Curr;}

  

Linked list focus problem, a lot of polygon, consider intersect disjoint, ring no ring + Find Leaves of Binary Tree (Java)

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.