Binary Tree Paths Solutions in C++
Number 257
Difficulty Easy
Acceptance 51.5%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/binary-tree-paths/// Author : Calinescu Valentin, Hao Chen// Date : 2015-10-23/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:vector<string> TreePaths;void DFS(TreeNode* node, string answer){answer += "->" + to_string(node->val);if(node->left == NULL && node->right == NULL)TreePaths.push_back(answer);else{if(node->left != NULL)DFS(node->left, answer);if(node->right != NULL)DFS(node->right, answer);}}vector<string> binaryTreePaths(TreeNode* root) {if(root != NULL){DFS(root, "");for(int i = 0; i < TreePaths.size(); i++)TreePaths[i].erase(TreePaths[i].begin(), TreePaths[i].begin() + 2);}return TreePaths;}};// Another more clear DFS implementationclass Solution {public:void binaryTreePathsHelper(TreeNode* root, vector<int> solution, vector<string>& result ) {if (!root) return;solution.push_back(root->val);//meet the leaf node, shape a path into the resultif (root->left==NULL && root->right==NULL){if(solution.size()>0){stringstream ss;for(int i=0; i<solution.size(); i++){ss << solution[i] << (i<solution.size()-1 ? "->":"");}result.push_back(ss.str());}return;}binaryTreePathsHelper(root->left, solution, result);binaryTreePathsHelper(root->right, solution, result);}vector<string> binaryTreePaths(TreeNode* root) {vector<string> result;vector<int> solution;binaryTreePathsHelper(root, solution, result);return result;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-paths/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <string>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursive/// Time Complexity: O(n), where n is the node's number in the tree/// Space Complexity: O(h), where h is the height of the treeclass Solution {public:vector<string> binaryTreePaths(TreeNode* root) {vector<string> res;if(root == NULL)return res;if(root->left == NULL && root->right == NULL){res.push_back(to_string(root->val));return res;}vector<string> leftPaths = binaryTreePaths(root->left);for(int i = 0 ; i < leftPaths.size() ; i ++)res.push_back(to_string(root->val) + "->" + leftPaths[i]);vector<string> rightPaths = binaryTreePaths(root->right);for(int i = 0 ; i < rightPaths.size() ; i ++)res.push_back(to_string(root->val) + "->" + rightPaths[i]);return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-paths/description//// Author : liuyubobobo/// Time : 2018-11-17#include <iostream>#include <string>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Non-Recursive/// Using Stack////// Time Complexity: O(n), where n is the node's number in the tree/// Space Complexity: O(h), where h is the height of the treeclass Solution {public:vector<string> binaryTreePaths(TreeNode* root) {vector<string> res;if(root == NULL)return res;stack<pair<TreeNode*, string>> stack;stack.push(make_pair(root, to_string(root->val)));while(!stack.empty()){TreeNode* cur = stack.top().first;string s = stack.top().second;stack.pop();if(!cur->left && !cur->right)res.push_back(s);if(cur->left)stack.push(make_pair(cur->left, s + "->" + to_string(cur->left->val)));if(cur->right)stack.push(make_pair(cur->right, s + "->" + to_string(cur->right->val)));}return res;}};int main() {return 0;}