← 返回 meta 的题目列表Validate Node Relation
类型:online_judge
Given a tree structure, you need to verify if a certain node satisfies the condition of having only a parent node. The relationships between nodes are defined by given parent-child connections. Please implement a method that takes the root of the tree and the target node as input, and returns a boolean indicating whether the target node has only a parent node. Requirements: O(n) time complexity and O(1) space complexity.
Example:
Input: Tree: 1 /
2 3
4 Target Node: 2
Output: False
Input: Tree: 1 /
2 3
4 Target Node: 4
Output: True
Example
Input
Input:
Tree:
1
/ \
2 3
\
4
Target Node: 2