vector<string> binaryTreePaths(TreeNode* root) { vector<string> ans; if (!root){ return ans; } string rootStr = to_string(root->val); if (!root->left&&!root->right){ ans.push_back(rootStr); return ans; } if (root->left){ vector<string> left = binaryTreePaths(root->left); for (int i=0; i<left.size(); i++){ left[i].insert(0, rootStr+"->"); } ans.insert(ans.begin(), left.begin(), left.end()); } if (root->right){ vector<string> right = binaryTreePaths(root->right); for (int i=0; i<right.size(); i++){ right[i].insert(0, rootStr+"->"); } ans.insert(ans.begin(), right.begin(), right.end()); } return ans; }
vector<string> binaryTreePaths(TreeNode* root) { vector<string> s; TreeNode *left; TreeNode *right; if (root != NULL) { int val = root->val; left = root->left; right = root->right; if (left == NULL && right == NULL) { s.push_back(to_string(val)); return s; } vector<string> left_s = binaryTreePaths(left); vector<string> right_s = binaryTreePaths(right); string str = to_string(val) + "->"; for (int i = 0; i < left_s.size(); i++) { left_s[i] = str + left_s[i]; } for (int i = 0; i < right_s.size(); i++) { right_s[i] = str + right_s[i]; } left_s.insert(left_s.end(), right_s.begin(), right_s.end()); s = left_s; } return s; }
vector<string> binaryTreePaths(TreeNode* root) { vector<string> l,r,ans; if(root==NULL){ return ans; } if(root->left==NULL&&root->right==NULL){ char c[256]; sprintf(c,"%d",root->val); string s=c; ans.push_back(s); return ans; } l=binaryTreePaths(root->left); r=binaryTreePaths(root->right); for(int i=0;i<l.size();i++){ char c[256]; sprintf(c,"%d->",root->val); string s=c; ans.push_back(s+l[i]); } for(int i=0;i<r.size();i++){ char c[256]; sprintf(c,"%d->",root->val); string s=c; ans.push_back(s+r[i]); } return ans; }
vector<string> binaryTreePaths(TreeNode* root) { vector<string> ret; if(root == NULL) return ret; if(root->left == NULL && root->right == NULL) ret.push_back(to_string(root->val)); string head = to_string(root->val) + "->"; for(auto item:binaryTreePaths(root->left)) ret.push_back(head+item); for(auto item:binaryTreePaths(root->right)) ret.push_back(head+item); return ret; }
vector<string> binaryTreePaths(TreeNode* root) { if (!root) return {}; if (!root->left && !root->right) return {std::to_string(root->val)}; // get subtree paths vector<string> left = binaryTreePaths(root->left); vector<string> right = binaryTreePaths(root->right); left.insert( left.end(), right.begin(), right.end()); // append root to each path for (auto &p: left){p = std::to_string(root->val) + "->" + p;} return left; }
int main() { struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode *)); root->val = 1; struct TreeNode *node1_1 = (struct TreeNode *)malloc(sizeof(struct TreeNode *)); node1_1->val = 2; root->left = node1_1; struct TreeNode *node1_2 = (struct TreeNode *)malloc(sizeof(struct TreeNode *)); node1_2->val = 3; root->right = node1_2; node1_2->left = NULL; node1_2->right = NULL; struct TreeNode *node2_1 = (struct TreeNode *)malloc(sizeof(struct TreeNode *)); node2_1->val = 5; node1_1->left = NULL; node1_1->right = node2_1; node2_1->left = NULL; node2_1->right = NULL; int returnSize = 0; char** paths = binaryTreePaths(root, &returnSize); assert(returnSize == 2); assert(strcmp(paths[0], "1->2->5") == 0); assert(strcmp(paths[1], "1->3") == 0); return 0; }
vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if (root == NULL) return res; string s_val = intToString(root->val); if (root->left == NULL && root->right == NULL) { res.push_back(s_val); return res; }; vector<string> right, left; right = binaryTreePaths(root->right); left = binaryTreePaths(root->left); for (int i(0); i < left.size(); i++) res.push_back(s_val + "->" + left[i]); for (int i(0); i < right.size(); i++) res.push_back(s_val + "->" + right[i]); return res; }
void binaryTreePaths(TreeNode *root, string visited, vector<string>& result) { if(root->left == nullptr && root->right == nullptr) { visited += to_string(root->val); result.push_back(visited); return; } if(root->left) binaryTreePaths(root->left, visited + to_string(root->val) + "->", result); if(root->right) binaryTreePaths(root->right, visited + to_string(root->val) + "->", result); return; }
vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; if(root == nullptr) return result; binaryTreePaths(root, "", result); return result; }
vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) return {}; // base case vector<string> result_left = binaryTreePaths(root -> left); vector<string> result_right = binaryTreePaths(root -> right); for (auto& result : result_right) { result_left.push_back(result); } for (auto& result : result_left) { result = to_string(root -> val) + "->" + result; } if (result_left.size() == 0) { result_left.push_back(to_string(root -> val)); } return result_left; }
/** * @param root the root of the binary tree * @return all root-to-leaf paths */ vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) { return vector<string>(); } if (root->left == NULL && root->right == NULL) { return vector<string>(1, std::to_string(root->val)); } vector<string> all_ways, ways; if (root->left != NULL) { ways = binaryTreePaths(root->left); all_ways.insert(all_ways.end(), ways.begin(), ways.end()); } if (root->right != NULL) { ways = binaryTreePaths(root->right); all_ways.insert(all_ways.end(), ways.begin(), ways.end()); } for (int i = 0; i < all_ways.size(); i++) { all_ways[i] = std::to_string(root->val) + "->" + all_ways[i]; } return all_ways; }