Leetcode 257. Binary Tree Paths
cpp代码,easy的,寻找路径,只需要传递string,然后在叶子节点进行加入vector即可。
/** * 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> binaryTreePaths(TreeNode* root) { vector<string> v; getPath(true, v, root, ""); return v; } void getPath(bool start, vector<string>& v, TreeNode* root, string str) { if(!root) return; if(!start) str += "->"; str += to_string(root->val); if(!root->left && !root->right) v.push_back(str); getPath(false, v, root->left, str); getPath(false, v, root->right, str); } };