DFS with States
Prereq: Recursion, DFS on Tree
Let's reinforce our understanding of the concept of "states" with another example.
Ternary Tree Paths
Given a ternary tree (each node of the tree can have up to three children), find all the root-to-leaf paths.

Try it yourself
Solution
We use path to keep track of the nodes we have visited so far to reach the current node and use it to construct our solution when we reach the leaf nodes.
1def ternary_tree_paths(root):
2 # dfs helper function
3 def dfs(root, path, res):
4 # exit condition: when a leaf node is reached, append the path to the results
5 if all(c is None for c in root.children):
6 res.append('->'.join(path + [str(root.val)]))
7 return
8
9 # DFS on each non-null child
10 for child in root.children:
11 if child is not None:
12 dfs(child, path + [str(root.val)], res)
13
14 res = []
15 if root: dfs(root, [], res)
16 return res
171private static void dfs(Node<Integer> root, ArrayList<String> path, ArrayList<String> res) {
2 // exit condition: when a leaf node is reached, append the path to the results
3 if (root.children.size() == 0) {
4 ArrayList<String> fullPath = new ArrayList<>(path);
5 fullPath.add(Integer.toString(root.val));
6 res.add(String.join("->", fullPath));
7 return;
8 }
9 // DFS on each non-null child
10 for (Node<Integer> child : root.children) {
11 if (child != null) {
12 ArrayList<String> pathCopy = new ArrayList<>(path);
13 pathCopy.add(Integer.toString(root.val));
14 dfs(child, pathCopy, res);
15 }
16 }
17}
18
19public static List<String> ternaryTreePaths(Node<Integer> root) {
20 ArrayList<String> res = new ArrayList<>();
21 if (root != null) dfs(root, new ArrayList<String>(), res);
22 return res;
23}
241function dfs(root, path, res) {
2 // exit condition: when a leaf node is reached, append the path to the results
3 if (root.children.length === 0) {
4 const fullPath = [...path, root.val];
5 res.push(fullPath.join('->'));
6 return;
7 }
8 // DFS on each non-null child
9 for (const child of root.children) {
10 if (child) {
11 const path_copy = Array.from(path);
12 path_copy.push(root.val);
13 dfs(child, path_copy, res);
14 }
15 }
16}
17
18function ternaryTreePaths(root) {
19 let res = [];
20 if (root) dfs(root, [], res);
21 return res;
22}
231void dfs(Node<int>* root, std::vector<std::string> path, std::vector<std::string>& res) {
2 if (root->children.size() == 0) {
3 path.emplace_back(std::to_string(root->val));
4 std::string s = "";
5 for (int i = 0; i < path.size(); i++) {
6 if (i > 0) {
7 s += "->";
8 }
9 s += path[i];
10 }
11 res.emplace_back(s);
12 return;
13 }
14 for (auto& child: root->children) {
15 if (child) {
16 std::vector<std::string> path_copy(path);
17 path_copy.emplace_back(std::to_string(root->val));
18 dfs(child, path_copy, res);
19 }
20 }
21}
22
23std::vector<std::string> ternary_tree_paths(Node<int>* root) {
24 std::vector<std::string> res;
25 if (root) dfs(root, {}, res);
26 return res;
27}
281private static void Dfs(Node<int> root, List<string> path, List<string> res)
2{
3 // exit condition: when a leaf node is reached, append the path to results
4 if (root.children.Count == 0)
5 {
6 List<string> fullPath = new List<string>(path);
7 fullPath.Add(root.val.ToString());
8 res.Add(string.Join("->", fullPath));
9 return;
10 }
11 // DFS on each non-null child
12 foreach (Node<int> child in root.children)
13 {
14 if (child != null)
15 {
16 List<string> pathCopy = new List<string>(path);
17 pathCopy.Add(root.val.ToString());
18 Dfs(child, pathCopy, res);
19 }
20 }
21}
22
23public static List<string> TernaryTreePaths(Node<int> root)
24{
25 List<string> res = new List<string>();
26 if (root != null) Dfs(root, new List<string>(), res);
27 return res;
28}
291func dfs(root Node, path []string, res *[]string) {
2 // exit condition: when a leaf node is reached, append the path to results
3 if len(root.children) == 0 {
4 fullPath := append(path, strconv.Itoa(root.val))
5 *res = append(*res, strings.Join(fullPath, "->"))
6 return
7 }
8
9 // DFS on each non-null child
10 for _, child := range root.children {
11 dfs(child, append(path, strconv.Itoa(root.val)), res)
12 }
13}
14
15func ternaryTreePaths(root Node) []string {
16 var result []string
17 dfs(root, []string{}, &result)
18 return result
19}
20Saving Space
In the recursive call in the previous solution, we create a new list each time we recurse with path + [root.val]. This is not space-efficient because creating a new list involves allocating new space in memory and copying over each element. A more efficient method is to use a single list, path, and push and pop according to the call stack.
Feel free to revisit the recursion section if you need a review of the call stack.
Expand 5 lines ... | |||||
6 | 6 | ||||
7 | 7 |
| |||
8 | 8 |
| |||
9 | - | | |||
9 | + | | |||
10 | + |
| |||
11 | + |
| |||
10 | 12 | ||||
11 | 13 |
| |||
12 | 14 |
| |||
Expand 1 lines ... |
1 | 1 | | |||
2 | 2 |
| |||
3 | - |
| |||
3 | + |
| |||
4 | - |
| |||
4 | + |
| |||
5 | - |
| |||
5 | + |
| |||
6 | 6 |
| |||
7 | 7 |
| |||
8 | 8 | ||||
9 | 9 |
| |||
10 | 10 |
| |||
11 | - |
| |||
11 | + |
| |||
12 | - |
| |||
12 | + |
| |||
13 | - |
| |||
13 | + |
| |||
14 | 14 |
| |||
15 | 15 |
| |||
16 | 16 |
| |||
Expand 6 lines ... |
1 | 1 | | |||
2 | 2 |
| |||
3 | - |
| |||
3 | + |
| |||
4 | - |
| |||
4 | + |
| |||
5 | + |
| |||
6 | + |
| |||
5 | 7 |
| |||
6 | 8 |
| |||
7 | 9 |
| |||
8 | 10 |
| |||
9 | - | | |||
11 | + |
| |||
10 | - |
| |||
12 | + |
| |||
11 | - |
| |||
13 | + |
| |||
12 | 14 |
| |||
13 | 15 |
| |||
14 | 16 |
| |||
Expand 6 lines ... |
Expand 8 lines ... | |||||
9 | 9 |
| |||
10 | 10 |
| |||
11 | 11 | | |||
12 | + | | |||
12 | 13 |
| |||
13 | 14 |
| |||
14 | 15 |
| |||
15 | 16 |
| |||
16 | - |
| |||
17 | + | | |||
17 | - | | |||
18 | + |
| |||
18 | - |
| |||
19 | + |
| |||
19 | 20 |
| |||
20 | 21 |
| |||
21 | 22 |
| |||
22 | 23 | ||||
23 | 24 | | |||
24 | 25 |
| |||
25 | - | | |||
26 | + | | |||
26 | 27 |
| |||
27 | 28 |
|
Expand 2 lines ... | |||||
3 | 3 |
| |||
4 | 4 |
| |||
5 | 5 |
| |||
6 | - | | |||
6 | + |
| |||
7 | - |
| |||
7 | + | | |||
8 | - | | |||
8 | + |
| |||
9 | 9 |
| |||
10 | 10 |
| |||
11 | 11 |
| |||
Expand 1 lines ... | |||||
13 | 13 |
| |||
14 | 14 |
| |||
15 | 15 |
| |||
16 | - | | |||
16 | + |
| |||
17 | - |
| |||
17 | + |
| |||
18 | - |
| |||
18 | + |
| |||
19 | 19 |
| |||
20 | 20 |
| |||
21 | 21 |
| |||
Expand 7 lines ... |
1 | - | | |
1 | + | | |
2 | 2 |
| |
3 | 3 | | |
4 | - | | |
4 | + | | |
5 | - | | |
5 | + | | |
6 | + | | |
6 | 7 |
| |
7 | 8 |
| |
8 | 9 |
| |
9 | 10 |
| |
10 | - | | |
11 | + | | |
12 | + |
| |
13 | + | | |
11 | 14 |
| |
12 | 15 |
| |
13 | 16 | ||
14 | 17 | | |
15 | 18 |
| |
16 | - |
| |
19 | + |
| |
20 | + |
| |
17 | 21 |
| |
18 | 22 |
|









