Leetcode 112. Path Sum

Source: Internet
Author: User

Whether there is a root-to-leaf node and equals the given number. Idea: Recursion. Determine if the left son or right son exists this path (sum becomes sum-root->val). Ver0: Recursion to the end will be the left and right son of the leaf node, so to have root==null judgment
1 classSolution {2  Public:3     BOOLHaspathsum (treenode* root,intsum) {4         if(root==NULL) {//error5             if(Sum = =0)return true;6             Else return false;7         }8         //if (sum < root->val) return false;9         returnHaspathsum (root->left,sum-root->val) | | Haspathsum (root->right,sum-root->val);Ten     } One};

WA:

Input:[] 0 Output:true expected:false Ver1: Whether the original tree is empty additionally judged.
1 classSolution {2  Public:3     BOOLHaspathsum (treenode* root,intsum) {4         if(Root==null)return false;5         returnHelp (root,sum);6         7     }8     9     BOOLHelp (treenode* root,intsum) {Ten         if(root==NULL) { One             if(Sum = =0)return true; A             Else return false; -         } -         //if (sum < root->val) return false; the         returnHelp (root->left,sum-root->val) | | Help (root->right,sum-root->val);//error -     } -};

WA:

Input:[1] Output:true expected:false mistakenly treats only one son's node as a leaf node. Ver2:
1 classSolution {2  Public:3     BOOLHaspathsum (treenode* root,intsum) {4         if(Root==null)return false;5         returnHelp (root,sum);6         7     }8     9     BOOLHelp (treenode* root,intsum) {Ten         if(root==NULL) { One             if(Sum = =0)return true; A             Else return false; -         } -         //if (sum < root->val) return false; the          -         if(Root->left && root->right)returnHelp (root->left,sum-root->val) | | Help (root->right,sum-root->val); -         if(Root->left)returnHelp (root->left,sum-root->val); -         if(Root->right)returnHelp (root->right,sum-root->val);

Leaf-node: + if(Sum = =0)return true;//error - return false; + A } at};

WA:

Input:[1] 1 Output:false expected:true in the case of a leaf node, the error is judged (i.e. sum==0) and should be sum==root->val. Ver3:
1 classSolution {2  Public:3     BOOLHaspathsum (treenode* root,intsum) {4         if(Root==null)return false;5         returnHelp (root,sum);6         7     }8     9     BOOLHelp (treenode* root,intsum) {Ten         //if (root==null) { One         //if (sum = = 0) return true; A         //else return false; -         // } -         //if (sum < root->val) return false; the          -         if(Root->left && root->right)returnHelp (root->left,sum-root->val) | | Help (root->right,sum-root->val); -         if(Root->left)returnHelp (root->left,sum-root->val); -         if(Root->right)returnHelp (root->right,sum-root->val); +         if(sum = = Root->val)return true; -         return false; +          A     } at};

other people's concise version of the code:
1 classSolution {2  Public:3     BOOLHaspathsum (treenode* root,intsum) {4         if(root==NULL)5             return false;6         if(Root->val = = Sum && root->left = = NULL && Root->right = =NULL)7             return true;8         returnHaspathsum (Root->left, sum-root->val) | | Haspathsum (Root->right, sum-root->val);9     }Ten};

Recursive to the last pass is the leaf node, not its left and right son, so there is no need to open the Help function.

If it is a node with only one son, it can also be processed (within the judgment of Root==null).

Leetcode 112. Path Sum

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.