Leetcode path sum

Source: Internet
Author: User

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.

Recursive Implementation

Judge from the root of the tree

  • If the current leaf node is equal to the remaining sum, the program returns true
  • If the current node is empty, false is returned.
  • In addition to the preceding conditions, return the results or values of the left and right subnodes that are recursively called, and pass in the values after the remaining and current node values are deducted.
class Solution {public:    bool hasPathSum(TreeNode *root, int sum) {        if(root == NULL) return false;        if(root->left == NULL && root->right == NULL && sum == root->val) return true;        else return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right, sum-root->val);    }};
class Solution {public:    bool hasPathSum(TreeNode *root, int sum) {        if(root == NULL) return false;        if(root->left == NULL && root->right == NULL) return sum == root->val;        sum-=root->val;        return hasPathSum(root->left,sum) || hasPathSum(root->right, sum);    }};
Recursive Implementation

Non-recursive traversal (based on hierarchical traversal)

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */#include <iostream>#include <queue>using namespace std;struct TreeNode{    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x): val(x), left(NULL),right(NULL){}};struct TreeNodeSum{    TreeNode *node;    int sum;    TreeNodeSum(TreeNode* node_, int sum_ ):node(node_),sum(sum_){}};bool hasPathSum(TreeNode *root, int sum) {    if(root == NULL) return false;    queue<TreeNodeSum *> que;    que.push(new TreeNodeSum(root,root->val));    int res = 0;    while(!que.empty()){        TreeNodeSum *tmp = que.front();que.pop();        TreeNode *node = tmp->node;        cout<<node->val<<endl;        if(!node->left && !node->right && tmp->sum == sum) return true;         if(node->left){            TreeNodeSum *nodeSum = new TreeNodeSum(node->left,node->left->val+tmp->sum);            que.push(nodeSum);        }        if(node->right ){            TreeNodeSum *nodeSum = new TreeNodeSum(node->right,node->right->val+tmp->sum);            que.push(nodeSum);        }    }    return false;}int main(){    TreeNode *root = new TreeNode(1);    TreeNode *root1 = new TreeNode(-2);    TreeNode *root2 = new TreeNode(-3);    TreeNode *root3 = new TreeNode(1);    TreeNode *root4 = new TreeNode(3);    TreeNode *root5 = new TreeNode(-2);    TreeNode *root6 = new TreeNode(-1);    root->left = root1;    root->right = root2;    root1->left = root3;    root1->right = root4;    root2->left = root5;    root3->left = root6;    cout<Non-Recursive Implementation

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.