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 are:
["1->2->5", "1->3"]
/** * 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> res; if(!root) return res; stack<pair<TreeNode*, string>> stack_node; stack_node.push(make_pair(root, to_string(root->val))); while(!stack_node.empty()) { pair<TreeNode*, string> snode = stack_node.top(); stack_node.pop(); if (!snode.first->left and !snode.first->right) { res.push_back(snode.second); } if(snode.first->right) stack_node.push(make_pair(snode.first->right, snode.second + "->" + to_string(snode.first->right->val))); if(snode.first->left) stack_node.push(make_pair(snode.first->left, snode.second + "->" + to_string(snode.first->left->val))); } return res; } };
class Solution { public: string VecInt2Str(vector<pair<TreeNode*, int>>& a) { vector<pair<TreeNode*, int>>::iterator ff = --a.end(), it; ostringstream ostr; for(it=a.begin(); it<ff; ++it) { ostr<<it->first->val; ostr<<"->"; } ostr<<it->first->val; return ostr.str(); } vector<string> binaryTreePaths(TreeNode* root) { typedef pair<TreeNode*, int> P_NODE; vector<P_NODE> flag; flag.push_back(make_pair(root, 0)); vector<string> res; if(root == NULL) return vector<string> (); P_NODE pp; while(!flag.empty()) { if(!(flag.back().first->left || flag.back().first->right)) { res.push_back(VecInt2Str(flag)); flag.pop_back(); } else if(flag.back().second == 0) { flag.back().second = 1; if(flag.back().first->left) flag.push_back(make_pair(flag.back().first->left, 0)); } else if(flag.back().second == 1) { flag.back().second = 2; if(flag.back().first->right) flag.push_back(make_pair(flag.back().first->right, 0)); } else { flag.pop_back(); } } return res; } };
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] res, temp, node = [], [str(root.val)], [[root, 0]] while node: if not (node[-1][0].left or node[-1][0].right): res.append("->".join(temp)) node.pop() temp.pop() elif node[-1][-1] == 0: node[-1][-1] = 1 if node[-1][0].left: node.append([node[-1][0].left, 0]) temp.append(str(node[-1][0].val)) elif node[-1][-1] == 1: node[-1][-1] = 2 if node[-1][0].right: node.append([node[-1][0].right, 0]) temp.append(str(node[-1][0].val)) else: node.pop() temp.pop() return res
void binaryTreePaths(vector<string>& result, TreeNode* root, string t) { if(!root->left && !root->right) { result.push_back(t); return; } if(root->left) binaryTreePaths(result, root->left, t + "->" + to_string(root->left->val)); if(root->right) binaryTreePaths(result, root->right, t + "->" + to_string(root->right->val)); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; if(!root) return result; binaryTreePaths(result, root, to_string(root->val)); return result; }
# dfs + stack def binaryTreePaths1(self, root): if not root: return [] res, stack = [], [(root, "")] while stack: node, ls = stack.pop() if not node.left and not node.right: res.append(ls+str(node.val)) if node.right: stack.append((node.right, ls+str(node.val)+"->")) if node.left: stack.append((node.left, ls+str(node.val)+"->")) return res # bfs + queue def binaryTreePaths2(self, root): if not root: return [] res, queue = [], collections.deque([(root, "")]) while queue: node, ls = queue.popleft() if not node.left and not node.right: res.append(ls+str(node.val)) if node.left: queue.append((node.left, ls+str(node.val)+"->")) if node.right: queue.append((node.right, ls+str(node.val)+"->")) return res # dfs recursively def binaryTreePaths(self, root): if not root: return [] res = [] self.dfs(root, "", res) return res def dfs(self, root, ls, res): if not root.left and not root.right: res.append(ls+str(root.val)) if root.left: self.dfs(root.left, ls+str(root.val)+"->", res) if root.right: self.dfs(root.right, ls+str(root.val)+"->", res)
def binaryTreePaths(self, root): if not root: return [] return [str(root.val) + '->' + path for kid in (root.left, root.right) if kid for path in self.binaryTreePaths(kid)] or [str(root.val)]