Path Sum, pathsum
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/41910495
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and
sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
Return true, as there exist a root-to-leaf path5->4->11->2Which sum is 22.
Ideas:
(1) The question is to give a binary tree and an integer (each node has a number) and determine whether there is a path from the root to the leaf node in the binary tree so that the sum of the paths is equal to the given integer.
(2) I think this question mainly describes the familiarity and use of common data structures "Stacks. It includes the main features of Stack: advanced post-release, Stack's peek () method, Stack's pop () method, and Stack's push () method.
(3) first, we can see from the analysis that to obtain the path from the root of the tree to the leaf node, We must traverse the tree as follows: traverse through the root-> left-> right method.
(4) Next, we need to consider how to continue when we traverse to the leaf node and the sum of the path values is not equal to the given value.
(5) Again, this article uses stacks to store nodes on the path, because when traversing to the leaf node, the sum of node values on the path is not equal to the given value, you only need to remove the leaf node. First, the root node is empty. If it is null, null is returned. If there is only one root node, a special judgment is required. Then, if the root node is not empty, push the root node into the stack (to make the entire tree node be traversed, as long as there are elements in the stack), and save the value of the node stored in the stack with the temporary variable count, if the stack is not empty, the system cyclically extracts the top element of the stack. If this element has a left child, it pushes the left child into the stack and adds the value to the count; if the element has a right Child, the right child is pushed into the stack and the value is added to the count. If the element has no left or right child, and the element is not the root of the tree, if the count value is the same as the given value, true is returned. Otherwise, the element is removed from the stack and the corresponding value is subtracted from the count value.
(6) In (5), note that each node popped up from the stack must be saved in a temporary Set, in addition, the left and right children of the node are stored in the stack to determine whether the left and right children are in the Set. This ensures that each node only enters the stack once. Otherwise, an endless loop is generated.
(7) Note: The Stack's peek () method is to retrieve the top elements of the stack without removing them from the stack. The Stack's pop () method is to retrieve the top elements of the stack, and remove it from the stack. The stack push () method is to push the elements into the stack. The HashSet contains () method can quickly determine whether an element is in the Set.
(8) Pay attention to the above aspects and you can easily get the correct answer. This question is actually not difficult. It mainly focuses on the use of stacks in the data structure and the ability to analyze problems.
(9) I hope this article will help you. Thank you.
The algorithm code is implemented as follows:
Public static boolean hasPathSum (TreeNode root, int sum) {if (root = null) return false; if (root! = Null & root. left = null & root. right = null & root. val = sum) return true; // each element can only be Stack once <TreeNode> stack = new Stack <TreeNode> (); // ensure that the element value is Set <TreeNode> set = new HashSet <TreeNode> (); int count = 0; stack. push (root); count + = root. val; while (stack. size ()! = 0) {TreeNode top = stack. peek (); if (top. left! = Null &&! Set. contains (top. left) {stack. push (top. left); count + = top. left. val;} else if (top. right! = Null &&! Set. contains (top. right) {stack. push (top. right); count + = top. right. val;} else {if (top! = Root & top. left = null & top. right = null & count = sum) {return true;} else {TreeNode pop = stack. pop (); set. add (pop); count = count-pop. val ;}}return false ;}