Path Sum paths and (note: Templates with each path are included: code for two different forms of expression)

Source: Internet
Author: User

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 and sum = 22,
              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.

The main idea: to determine if there is a path, and its sum is equal to the given sums.


1. My solution (Recursive)

/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (NULL) , right (NULL) {}}; */class Solution {Public:bool pathsum (treenode* root, int all, int sum) {if (root->left== NULL && Roo            T->right = = NULL) {if (all+root->val = = sum) return true;        else return false; } if (root->left== NULL | | root->right = = NULL) {//The case where there is only one child node must be processed, the empty bar cannot be counted as a path treenode* t = root-            >left!=NULL?root->left:root->right;        Return Pathsum (t,all+root->val,sum); }else{return Pathsum (root->left,all+root->val,sum) | |        Pathsum (root->right,all+root->val,sum);        }} bool Haspathsum (treenode* root, int sum) {if (!root) return false;    Return Pathsum (root,0,sum); }};

Make improvements to the above. The main point here is that there is only one left/right node for a node to handle.

The above approach is that if you encounter only one child node, then the child node is passed down;

The following practice is that if you encounter an empty (only one child node, then the other child node is empty,), then this fork does not belong to a path, the direct false.

Because only the leaf nodes (left and right are empty), it is a path.

/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode (int x): Val (x), left (NULL) , right (NULL) {}}; */class Solution {public:    bool Pathsum (treenode* root, int all, int sum) {        if (root==null)        return false;        if (root->left== null && root->right = = NULL) {            if (all+root->val = = SUM)            return true;            else            return false;        }        Return Pathsum (root->left,all+root->val,sum) | | Pathsum (root->right,all+root->val,sum);            }    BOOL Haspathsum (treenode* root, int sum) {        if (!root)        return false;        Return Pathsum (root,0,sum);    };



2. Other people's solution (recursion)

BOOL Haspathsum (TreeNode *root, int sum) {        if (root = NULL) return false;        if (root->val = = Sum && root->left = =  Null && root->right = = null) return true;        Return Haspathsum (Root->left, sum-root->val) | | Haspathsum (Root->right, sum-root->val);    }


Path Sum paths and (note: Templates with each path are included: code for two different forms of expression)

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.