Measure the Longest Root-to-Leaf Node Path

Quick Overview

Measure binary-tree maximum depth as the node count on its longest root-to-leaf path.

Measure the Longest Root-to-Leaf Node Path

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Find the maximum depth of a binary tree. Depth counts nodes, so it is the greatest number of nodes on any path beginning at the root and ending at a leaf. An empty tree has depth 0. ```cpp int maxDepth(TreeNode* root); ``` The supplied `TreeNode` contains `val`, `left`, and `right`. Brackets in examples describe its level-order structure, not an array parameter. ### Examples ```text Input: root = [6,2,9,null,4,null,11,null,5] Output: 4 ``` The path through values 6,2,4,5 contains four nodes. ```text Input: root = [] Output: 0 ``` ### Constraints - There are between 0 and 10000 nodes. - Each node value is between -100 and 100, inclusive.

Overview: Measure binary-tree maximum depth as the node count on its longest root-to-leaf path.

Community answers

Answer by Coderka14

class Solution { public: int maxDepth(TreeNode* root) { if (root == nullptr) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return 1 + max(leftDepth, rightDepth); } };
Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Find the maximum depth of a binary tree. Depth counts nodes, so it is the greatest number of nodes on any path beginning at the root and ending at a leaf. An empty tree has depth 0.

int maxDepth(TreeNode* root);

The supplied TreeNode contains val, left, and right. Brackets in examples describe its level-order structure, not an array parameter.

Examples

Input: root = [6,2,9,null,4,null,11,null,5]
Output: 4

The path through values 6,2,4,5 contains four nodes.

Input: root = []
Output: 0

Constraints

  • There are between 0 and 10000 nodes.
  • Each node value is between -100 and 100, inclusive.