Chapter 3: Q19E (page 109)
As in the previous problem, you are given a binary tree T=(V,E) with designated root node. In addition, there is an array with a value for each node in V Define a new array as follows: for each ,
the maximum of the x-values associated with u’s descendants.
Give a linear-time algorithm that calculates the entire z-array.
Short Answer
A linear-time algorithm is as follows.
Input: BinaryTree T=(v,E)
Output: z[.] for each
Initialize z{i} to 0,
Begin procedure DFS ( )
While traversal
if node pops out of the stack
update the z values of the node u at the top of the stack
z{u} =max (z{u},x{v})
return z{u}