Here's the code you can't write clearly!
It's a concept called backtracking. Because the parameters here are written in the way of passing reference, if you change one branch line, the other will also change. When you stack again, you will add more NIMA.
In addition, it's not good to set the recursive base to an empty node here, because every time your stewardess points, it's not necessarily a leaf node, such as a node with only one son. If you see the recursive base and push it into the answer, it's a mess
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: void haha(TreeNode* node, string& s, vector<string>& ans){ if(!node){ ans.push_back(s); return; } if(node->left == nullptr && node->right == nullptr){ s += to_string(node->val); } else{ s += to_string(node->val); s += "->"; } haha(node->left, s, ans); haha(node->right, s, ans); return; } vector<string> binaryTreePaths(TreeNode* root) { string s ={}; vector<string> ans; haha(root, s, ans); return ans; } }; input [1,2,3,null,5] output ["1->2->","1->2->5","1->2->5","1->2->53","1->2->53"] Expected results ["1->2->5","1->3"]
Here, it is modified to string type, and then the string is superimposed, using the interface to_string.
In addition, the recursive base has been modified, so we need to judge more here, because there may be null pointers.
The backtracking here is the key to solve the problem!!!
Record the string before recursion, and then return to the original string after recursion!
There are also two points of knowledge here, that is, I originally wanted to use pop_back to go back, but this is not very easy to use, because the characters that may be deleted may be one or two. But I'm not very familiar with the interface of string before, so I'll review it here.
class Solution { public: void construct_paths(TreeNode* root, string path, vector<string>& paths) { if (root != nullptr) { path += to_string(root->val); if (root->left == nullptr && root->right == nullptr) { // The current node is a leaf node paths.push_back(path); // Add the path to the answer } else { path += "->"; // The current node is not a leaf node. Continue recursive traversal construct_paths(root->left, path, paths); construct_paths(root->right, path, paths); } } } vector<string> binaryTreePaths(TreeNode* root) { vector<string> paths; construct_paths(root, "", paths); return paths; } }; Author: LeetCode-Solution Link: https://leetcode-cn.com/problems/binary-tree-paths/solution/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.
Both the official explanation and the code Capriccio here do not use backtracking. At the same time, their string is not a reference passed, so let's know backtracking here.
class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { stack<TreeNode*> treeSt;// Save traversal nodes of the tree stack<string> pathSt; // Save nodes traversing the path vector<string> result; // Save final path collection if (root == NULL) return result; treeSt.push(root); pathSt.push(to_string(root->val)); while (!treeSt.empty()) { TreeNode* node = treeSt.top(); treeSt.pop(); // Remove from node string path = pathSt.top();pathSt.pop(); // Take out the path corresponding to the node if (node->left == NULL && node->right == NULL) { // Leaf node encountered result.push_back(path); } if (node->right) { // right treeSt.push(node->right); pathSt.push(path + "->" + to_string(node->right->val)); } if (node->left) { // Left treeSt.push(node->left); pathSt.push(path + "->" + to_string(node->left->val)); } } return result; } }; Iteration of code Capriccio
class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> paths; if (root == nullptr) { return paths; } queue<TreeNode*> node_queue; queue<string> path_queue; node_queue.push(root); path_queue.push(to_string(root->val)); while (!node_queue.empty()) { TreeNode* node = node_queue.front(); string path = path_queue.front(); node_queue.pop(); path_queue.pop(); if (node->left == nullptr && node->right == nullptr) { paths.push_back(path); } else { if (node->left != nullptr) { node_queue.push(node->left); path_queue.push(path + "->" + to_string(node->left->val)); } if (node->right != nullptr) { node_queue.push(node->right); path_queue.push(path + "->" + to_string(node->right->val)); } } } return paths; } }; Author: LeetCode-Solution Link: https://leetcode-cn.com/problems/binary-tree-paths/solution/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.
Above is breadth first play!