Google Interview Question: Descending Tree Definition and Operations

12 Views
No Comments

“””
// Write a node class to be used in a tree. A Node should contain exactly one integer and have zero or more children.
“””

“””
Define: a “descending tree” is a tree such that for each node n, the value of n is larger than the value of any descendant of n.

        1000
      /   |   \
   500   200   700
   / \     |     / \
300 100   ...   50  150

Write a function that takes a node which is the root of a tree and determines if it is the root of a descending tree.
“””

“””
Write a function that takes a node which is the root of a descending tree, and returns the largest leaf value.
“””

This Google interview set focuses on defining a flexible tree node class (each node stores one integer and has zero 或多个孩子), then introduces a special structure called a“descending tree.”The property is simple but strong: every node must be strictly larger than all of its descendants. Based on this definition, the first task is to implement a function to verify whether a given root satisfies this property, requiring a recursive check across all children. The second task is to return the largest leaf value in a valid descending tree, which boils down to exploring all leaf nodes and selecting the maximum.

END
 0