Sword refers to the offer-of a path in a two fork tree and is given a value

Source: Internet
Author: User

Enter a binary tree and an integer to print out all paths for the value of the node in the two-fork tree and for the input integer. A path is defined as a path from the root node of the tree down to the node through which the leaf nodes go.
/** * Enter a binary tree and an integer to print out all paths for the value of the node in the two-fork tree and for the input integer. * The path is defined as a path from the root node of the tree down to the node through which the leaf nodes go. * In fact, I did not think of the first sequence traversal, but with a similar deep search method, continue to go down until the leaf node is found * and then see if sum is equal to the target * but there is a problem is, after traversing the left child of a node, when traversing the right child, the left child was found to stay in the list. * One way to do this is to create an identical list before traversing, which is the extra space consumed. * With the first order traversal in fact the same way, but with a stack to maintain, after traversing a node of the left and right children, you need to put this point out of the stack. * @author Admin * */public class Pathsum {public static arraylist<arraylist<integer>> results=new ARRAYLIST&L T Arraylist<integer>> (); public static arraylist<arraylist<integer>> Findpath (TreeNode root,int target) {if (root==null| |        Target<root.val) {return results;        } findpath (Root,0,target,new arraylist<integer> ());          return results; } public static void Findpath (TreeNode current,int sum,int target,arraylist<integer> stack) {if (current!=null) {St Ack.add (Current.val); Sum+=current.val; if (current.left==null&&current.right==null) {if (sum==target) {///If Sum==target is added to the result set Results.add (new Arraylist<integer> (Stack)); }//If not equal, the stack is also required, so there is no return} if (Current.left!=null) {Findpath (current.left,sum,target,stack);} if (current.right!= NULL) {Findpath (current.right,sum,target,stack);}//current node out stack stack.remove (Stack.size ()-1); } }}

  

Sword refers to the offer-of a path in a two fork tree and is given a value

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.