Back to course home
0% completed
Vote For New Content
Find maximum depth of subtree (medium)
Problem Statement
Given the root
node of a binary tree and an integer val
, find the maximum depth of the subtree that starts from the node with the value equal to val
. If the node with value val
doesn't exist in the tree, return 0
.
The depth of a node is defined as the number of edges on the longest path from the node to a leaf.
Examples
Example 1
- Input: root =
[3, 5, 4, 2, null, 1, null, 6]
, val =5
- Expected Output:
3
- Explanation: The subtree starting from the node with value
5
has nodes5
,2
, and6
. The maximum depth of this subtree is3
.
Example 2
- Input: root =
[1, 2, 3, 4, null, null, 5, 6]
, val =3
- Expected Output:
2
- Explanation: The subtree starting from the node with value
3
has nodes3
and5
. The maximum depth of this subtree is2
.
Example 3
- Input: root =
[7, 3, 9, 1, 6, null, 8, null, null, null, 2]
, val =3
- Expected Output:
3
- Explanation: The subtree starting from the node with value
3
has nodes3
,6
, and2
. The maximum depth of this subtree is3
.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself