[Leetcode] [Java] Path Sum

Source: Internet
Author: User

Title:

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such this adding up all the values along the Path equals the given sum.

For Example:
Given The below binary tree sum = 22 and ,
              5             /             4   8           /   /           /  4         /  \              7    2      1

Return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Test Instructions:

Given a binary tree and a sum, determine if there is a path from the root node to the leaf node in the binary tree so that the values of all the nodes in the path are added equal to the sum of the constant.

Like what

Given the following binary tree and sum = 22 ,

              5             /             4   8           /   /           /  4         /  \              7    2      1
Returns true because there is a path from the root node to the leaf node in the tree 5->4->11->2their and for the.

Algorithm Analysis:

Method One:

The normal recursive method is to do, the recursive condition is to see Zuozi or right subtree has not satisfied the condition of the path, that is, the subtree path and equal to the current sum minus the current node value. The end condition is if the current node is empty, returns False if it is a leaf, and if the remaining sum equals the value of the current leaf, finds the path that satisfies the condition and returns true.

Method Two:

This is the typical BFS (breadth-first search) topic that leverages the queue.

Stores all nodes in one queue rollup, and stores the node values in another queue. If the current node is a leaf node, check whether the stored and values are target values.

AC Code:

Method One:

<span style= "Font-family:microsoft yahei;font-size:12px;" >//Recursive algorithm public class Solution {public  Boolean haspathsum (TreeNode root, int sum)    {          if (root = null) c4/>{              return false;          }          if (Root.left = = NULL && Root.right = = null)        {              if (Sum-root.val = = 0)            {                  return true;              }           }          Return (Haspathsum (Root.left, sum-root.val) | | haspathsum (root.right, Sum-root.val));      } </span>


Method Two:

<span style= "Font-family:microsoft yahei;font-size:12px;"  >//non-recursive algorithm public class Solution {public Boolean haspathsum (TreeNode root, int sum) {if (root = null) return         False        linkedlist<treenode> nodes = new linkedlist<treenode> ();         linkedlist<integer> values = new linkedlist<integer> ();        Nodes.Add (root);         Values.add (Root.val);            while (!nodes.isempty ()) {TreeNode Curr = Nodes.poll ();             int sumvalue = Values.poll ();            if (Curr.left = = NULL && Curr.right = = null && sumvalue==sum) {return true;                } if (Curr.left! = null) {Nodes.Add (curr.left);            Values.add (Sumvalue+curr.left.val);                } if (curr.right! = null) {Nodes.Add (curr.right);            Values.add (Sumvalue+curr.right.val);    }} return false; }}</SPAN> 

Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Path Sum

Related Article

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.