Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1/ 2 3 5
All root-to-leaf paths is:
["1->2->5", "1->3"]
Realize:
/**
* 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> vs;
if (root = NULL) return vs;
if (Root->left = = NULL && Root->right = = null) {
Char buff[124];
sprintf (Buff, "%d", root->val);
Vs.push_back (Buff);
return vs;
}
if (root->left) {
Vector<string> lefts = binarytreepaths (root->left);
for (int i = 0; i < lefts.size (); i++) {
Char *buff = new Char[lefts[i].size () +56];
sprintf (Buff, "%d->%s", Root->val, Lefts[i].c_str ());
Vs.push_back (Buff);
delete[] Buff;
}
}
if (root->right) {
vector<string> rights = Binarytreepaths (Root->right);
for (int i = 0; i < rights.size (); i++) {
Char *buff = new Char[rights[i].size () +56];
sprintf (Buff, "%d->%s", Root->val, Rights[i].c_str ());
Vs.push_back (Buff);
delete[] Buff;
}
}
return vs;
}
};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode257--binary Tree Paths