Sword refers to the path of the binary tree and a specific value in offer, and sword refers to the binary tree in offer.

Source: Internet
Author: User

Sword refers to the path of the binary tree and a specific value in offer, and sword refers to the binary tree in offer.

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]

Question link: http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca? Rp = 2 & ru =/ta/coding-interviews & qru =/ta/coding-interviews/question-ranking

Enter a binary tree and an integer to print the sum of the node values in the binary tree and all the paths of the input integers. The path is defined as a path formed from the root node of the tree down to the node through which the leaf node passes.


We start a level-1 search from the root node and press the node into the path for each search. Then, we can determine whether the node is a leaf node. If it reaches the leaf node, it determines whether the sum of the path meets the conditions. If the conditions are met, the path is saved.

Once we reach the bottom layer, we still need to trace back layer by layer, so we need to use the features of Deep Search and backtracking.

/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution{public:vector<vector<int> > FindPath(TreeNode* root,int expectNumber){if(root==nullptr) return ans;int sum = 0;vector<int> path;Find(root,path,sum,expectNumber);return ans;}void Find(TreeNode *root,vector<int> path,int sum,int expectNumber){sum+=root->val;path.push_back(root->val);bool isLeaf = (root->left==nullptr && root->right==nullptr);if(sum==expectNumber && isLeaf){ans.push_back(path);}if(root->left!=nullptr)Find(root->left,path,sum,expectNumber);if(root->right!=nullptr)Find(root->right,path,sum,expectNumber);path.pop_back();}private:vector<vector<int> > ans;};

Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source

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.