Given a binary tree, where an arbitary node has 2 parents i.e two nodes in the tree have the same child. Identify the defective node and remove an extra edge to fix the tree.
Example:
Input:
1
/ \
2 3
/ \ /
4 5
Output:
1 1
/ \ / \
2 3 or 2 3
/ \ / /
4 5 4 5
Explanation: We can remove either 3-5 or 2-5.
Input:
3
/ \
2 5
/ \ /
1 4
Output:
3
/ \
2 5
/ /
1 4
Explanation: In this case we can only remove 2-4 because if we remove 5-4 the BST will be invalid.
Design an algorithm and write code to figure our which trail you are hiking. There are lots of trails around you. And you are hiking, which means the distance between you and all the points are continue changing. It’s an open question. You need to define input and output yourself. I set the input as [trail id: points array] (point: {x,y,z}) as the points for all trails in my area. The output is the trail id you are hiking on.