257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
Example
Example 1:
Input:{1,2,3,#,5}
Output:["1->2->5","1->3"]
Explanation:
1
/ \
2 3
\
5
Example 2:
Input:{1,2}
Output:["1->2"]
Explanation:
1
/
2
Method 1:
- /**
- * 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:
- vector<string> binaryTreePaths(TreeNode* root) {
- vector<string> result;
- if (root == NULL) return result;
- helper(root, to_string(root->val), result);
- return result;
- }
- void helper(TreeNode* root, string path, vector<string> &result) {
- if (root == NULL) return;
- if (root->left == NULL && root->right == NULL) {
- result.push_back(path);
- return;
- }
- if (root->left != NULL) {
- helper(root->left, path + "->" + to_string(root->left->val), result);
- }
- if (root->right != NULL) {
- helper(root->right, path + "->" + to_string(root->right->val), result);
- }
- }
- };
Method 2:
- /**
- * 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:
- /**
- * @param root: the root of the binary tree
- * @return: all root-to-leaf paths
- */
- vector<string> binaryTreePaths(TreeNode * root) {
- vector<string> result;
- if (root == NULL) return result;
- vector<string> left = binaryTreePaths(root->left);
- vector<string> right = binaryTreePaths(root->right);
- for (auto l: left) {
- result.push_back(to_string(root->val) + "->" + l);
- }
- for (auto r: right) {
- result.push_back(to_string(root->val) + "->" + r);
- }
- if (result.size() == 0) result.push_back(to_string(root->val));
- return result;
- }
- };

Similar to Ignition, Café Casino also doesn’t offer a plethora of choices phrases of|in relation to} banking. Here, you will get get} your cash on and off net site} utilizing American Express, Visa, or Mastercard. Thus, the casino could be accessed from smartphones and 1xbet korea tablets - Windows, iOS, or Android. For fiat deposits, the welcome bonus is smaller - a 250% deposit match up to as} $1,500. But regardless of the limitation, the obtainable methods are entirely safe and secure.
ReplyDelete