The deleted/added/value-changed cases came to me pretty fast.
Clarify the definition of 'changed' and the tree properties, then propose a two-pass approach: first traverse both trees to build a map from key to node metadata (value, parent key), then compare the maps to count added, deleted, and modified nodes. Analyze time and space complexity, noting that O(N) time and O(N) space is optimal for arbitrary trees.
Pro tip: Mention that if keys are unique, you can avoid storing the entire tree by using a hash map; if keys can duplicate, you need to handle collisions by including parent information in the key or using a multi-map. Also, consider iterative traversal to avoid stack overflow for deep trees.
Ask whether keys are unique, whether node order among siblings matters, and confirm the definition of 'changed' (including parent change). Also clarify if the trees can be large and if recursion depth is a concern.
Decide on a traversal method (BFS or DFS) and a data structure to store node information. A hash map from key to (value, parent key) is efficient if keys are unique; otherwise, use a composite key or a multi-map.
Traverse the old tree, and for each node, record its key, value, and parent key (or null for root) in the chosen data structure. Count the total number of nodes in the old tree.
Traverse the new tree. For each node, check if its key exists in the old tree's map. If not, it's added (count++). If it exists, compare value and parent key; if different, it's changed (count++). Mark the key as seen. After traversal, any keys in the old map not seen are deleted (count++).
State that time complexity is O(N) where N is total number of nodes across both trees, and space complexity is O(N) for the hash map. Discuss edge cases: duplicate keys, root changes, deep trees (use iterative traversal), and memory constraints.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.