"Sword means offer" 17, binary tree and the path to a value

Source: Internet
Author: User

Title Description Enter a binary tree and an integer to print out all the paths of the node values in the two-fork tree and the input integers. A path is defined as a path from the root node of the tree down to the node through which the leaf nodes go. Analysis: The node of the team tree is worth the sum of the operations are mostly based on tree traversal operations, as long as the tree traversal operation slightly distorted, basically can solve the problem (personal view). We know that only the first order traversal of the tree is the first access to the root node, the key to this problem is how to save the nodes in the tree traversal process, the path satisfies the conditions to join the results, not meet the conditions of the path node how to re-search. When a node is accessed by a pre-order traversal, we add the node to the path and accumulate the value of that node. If the node is a leaf node and the value of the node in the path is exactly equal to the input data, the current path meets the requirements, we save him to the result to be returned, and if the current node is not a leaf node, we continue to access its child nodes. The recursive function automatically returns to its parent node after the current node access is complete. So we're going to delete the current node on the path before the function exits and subtract the value of the current node to ensure that the path is exactly the path from the node to the parent node when the parent node is returned (refer to the book "Sword Offer"). The code looks like this:
1 Importjava.util.ArrayList;2 /**3 Public class TreeNode {4 int val = 0;5 TreeNode left = null;6 TreeNode right = null;7  8 Public TreeNode (int val) {9 this.val = val;Ten   One     } A   - } - */ the  Public classSolution { -      PublicArraylist<arraylist<integer>> Findpath (TreeNode root,inttarget) { -arraylist<arraylist<integer>> res =NewArraylist<arraylist<integer>> (); -arraylist<integer> Path =NewArraylist<integer>() ; + Search (root, target,res,path); -          returnRes; +         } A     Private voidSearch (TreeNode root,intTarget, arraylist<arraylist<integer>> res,arraylist<integer>path) { at         if(root==NULL){ -             return ; -         } -target = target-Root.val; -         if(Root.left = =NULL&& Root.right = =NULL){ -             if(target = = 0){ in Path.add (root.val); -Res.add (NewArraylist<integer>(path)); toPath.remove (Path.size ()-1); +             } -             return; the         } * Path.add (root.val); $ search (Root.left,target,res,path);Panax Notoginseng search (Root.right,target,res,path); -Path.remove (Path.size ()-1) ; the           +     } A}

"Sword means offer" 17, binary tree and the path to 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.