For C++ solution, you loop through refernces and do if (child)
References are never null, and you would get an exception in this case. its not a good code. It doesnt compile for me.
Of course had to make changes, which is fine
Ability to provide a debugger within the solution terminal would be great!
one thing I really didn’t understand was the purpose on path.pop(). but I figured it out.
if we removed all the path.pop(), you will see that path only becomes additive. in each iteration of a loop, path is not reset, when we feed our path value into the next child, it will alter the path value of the earlier iteration. the next child, will change the path value for the previous parent node.
to demonstrate clearly try 2 things:
1.) remove all .pop() and console.log(path) after each .push(root.val) in both the loop and the if statement. you will see that path simply adds values on.
2.) add a console.log(path) after each .push(root.val) AND after each .pop() in the loop and if statement. you will see that as we go down the children, it becomes additive, growing our path until we arrive at the end. but as we traverse back up, we reset to an empty array at the highest level of our tree, 1 pop at a time back.
why not just reset path = []? because if we have a ternary tree where the children of the root node have multiple children as well, then their path’s would end up starting at the previous node, completely forgetting the previous path to get there from the root node. so, we only want to pop one at a time, so that each child of each node remember the previous path.
For the first solution you provide, I find it is unnecessary to make res a paramenter for recursive call. Would it be better if we set res a global variable?
def ternary_tree_paths(root: Node) → List[str]:
# WRITE YOUR BRILLIANT CODE HERE
totalpaths = []
stack = [(root,[root])]
visited = []
while stack:
curr_node,path = stack.pop() #if we reach the laf node, then we need to add the current path to all paths
if curr_node and not curr_node.children:
totalpaths.append(path)
continue
if curr_node in visited:
continue
visited.append(curr_node) #add the children with larger values to smaller values
childrs = curr_node.children[::-1]
for child in childrs:
stack.append((child,path + [child]))
output = []
for path in totalpaths:
temp =“”
for i,p in enumerate(path):
temp += str(p.val)
if i != len(path)-1:
temp +=“->”
output.append(temp)
return output
What is the difference between “path + [str(root.val)]” and “path.append(str(root.val))” in the second solution. Do those not accomplish the same thing?
For the python solution, if the tree contains only 1 node
Input 1 0
Output will be → 1, I think it is weird, for Java and C++ solution
it will output 1. Need to update python solution ?
function ternaryTreePaths(root) {
let result = [];
if (root.children.length > 0) {
for (const child of root.children) {
if (child) {
const res = ternaryTreePaths(child);
res.forEach((r) => result.push(`${root.val}->${r}`));
}
}
} else {
result.push(`${root.val}`);
}
return result;
}
Not clear why do we need the state at all. Wouldn’t this be simpler?
def find_paths(root: Node) → List[str]:
answers = []
if not root.children:
return [[str(root.val)]]
for child in root.children:
paths = find_paths(child)
for path in paths:
answers.append([str(root.val)] + path)
return answers
Sorry, but the link provided shows serialisation of a binary tree. It is not clear for me how to jump from the serialisation of a binary tree to such of a generic one.