def ternary_tree_paths(root):
# dfs helper function
def dfs(root, path, res):
# exit condition: when a leaf node is reached, append the path to the results
if all(c is None for c in root.children):
res.append('->'.join(path) + '->' + str(root.val))
return
# DFS on each non-null child
for child in root.children:
if child is not None:
dfs(child, path + [str(root.val)], res)
res = []
if root: dfs(root, [], res)
return res
private static void dfs(Node<Integer> root, ArrayList<String> path, ArrayList<String> res) {
// exit condition: when a leaf node is reached, append the path to the results
if (root.children.size() == 0) {
path.add(Integer.toString(root.val));
res.add(String.join("->", path));
return;
}
// DFS on each non-null child
for (Node<Integer> child : root.children) {
if (child != null) {
ArrayList<String> pathCopy = new ArrayList<>(path);
pathCopy.add(Integer.toString(root.val));
dfs(child, pathCopy, res);
}
}
}
public static List<String> ternaryTreePaths(Node<Integer> root) {
ArrayList<String> res = new ArrayList<>();
if (root != null) dfs(root, new ArrayList<String>(), res);
return res;
}
function dfs(root, path, res) {
// exit condition: when a leaf node is reached, append the path to the results
if (root.children.length === 0) {
path.push(root.val);
const cur_path = path.join('->');
res.push(cur_path);
return;
}
// DFS on each non-null child
for (const child of root.children) {
if (child) {
const path_copy = Array.from(path);
path_copy.push(root.val);
dfs(child, path_copy, res);
}
}
}
function ternaryTreePaths(root) {
let res = [];
if (root) dfs(root, [], res);
return res;
}
void dfs(Node<int>* root, std::vector<std::string> path, std::vector<std::string>& res) {
if (root->children.size() == 0) {
path.emplace_back(std::to_string(root->val));
std::string s = "";
for (int i = 0; i < path.size(); i++) {
if (i == path.size() - 1) {
s += path[i];
} else {
s += path[i] + "->";
}
}
res.emplace_back(s);
return;
}
for (auto& child: root->children) {
if (child) {
std::vector<std::string> path_copy(path);
path_copy.emplace_back(std::to_string(root->val));
dfs(child, path_copy, res);
}
}
}
std::vector<std::string> ternary_tree_paths(Node<int>* root) {
std::vector<std::string> res;
if (root) dfs(root, {}, res);
return res;
}
private static void Dfs(Node<int> root, List<string> path, List<string> res)
{
// exit condition: when a leaf node is reached, append the path to results
if (root.children.Count == 0)
{
path.Add(root.val.ToString());
res.Add(string.Join("->", path));
return;
}
// DFS on each non-null child
foreach (Node<int> child in root.children)
{
if (child != null)
{
List<string> pathCopy = new List<string>(path);
pathCopy.Add(root.val.ToString());
Dfs(child, pathCopy, res);
}
}
}
public static List<string> TernaryTreePaths(Node<int> root)
{
List<string> res = new List<string>();
if (root != null) Dfs(root, new List<string>(), res);
return res;
}
func dfs(root Node, path []string, res *[]string) {
// exit condition: when a leaf node is reached, append the path to results
if len(root.children) == 0 {
path = append(path, strconv.Itoa(root.val))
*res = append(*res, strings.Join(path, "->"))
return
}
// DFS on each non-null child
for _, child := range root.children {
dfs(child, append(path, strconv.Itoa(root.val)), res)
}
}
func ternaryTreePaths(root Node) []string {
var result []string
dfs(root, []string{}, &result)
return result
}