← 返回 netflix 的题目列表Report Each Binary-Tree Node's Level and Balance Status
类型:online_judge
Given the root of a binary tree, use DFS to report the following information for every node:
The node value;
Its depth level, where the root is at level 0;
Whether the node is balanced, isBalanced.
A node is balanced if and only if, in the subtree rooted at that node, the heights of its left and right subtrees differ by at most 1. An empty subtree has height 0, and a leaf has height 1.
Print nodes in DFS preorder. Print one line per node in the following format:
<node_value> <level> <true_or_false>
Input Format
The input is a level-order array representation of a binary tree:
An integer denotes a node value.
null denotes a missing node.
The input is guaranteed to be a valid level-order representation.
Example
Input:
3 9 20 null null 15 7
Output:
3 0 true
9 1 true
20 1 true
15 2 true
7 2 true
Constraints
0 <= n <= 100000
Node values are in [-10^9, 10^9]
Avoid recomputing subtree heights independently for every node.
Example
Input
3 9 20 null null 15 7
Output
3 0 true
9 1 true
20 1 true
15 2 true
7 2 true