# 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

Description
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.

Ideas

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;};`

Related Keywords:

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.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://topic.alibabacloud.com/a/sword-refers-to-the-path-of-the-binary-tree-and-a-specific-value-in-offer-and-sword-refers-to-the-binary-tree-in-offer_1_11_32460481.html", "fetch_time": 1726344343000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700651580.74/warc/CC-MAIN-20240914193334-20240914223334-00077.warc.gz", "warc_record_offset": 554270995, "warc_record_length": 15938, "token_count": 690, "char_count": 2944, "score": 3.515625, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.7039889097213745, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00059-of-00064.parquet"}