104.maximum-depth-of-binary-tree

題目

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

翻譯

尋找樹最大的深度是多少,如下圖就是 3。

Example1:

Example2:

思路

一、極限值/特殊狀況

二、哪種資料結構解

  • 樹狀

三、大概會怎麼解

  • 利用 max 紀錄最大深度

  • 使用遞迴

  • 遞迴函式內 count 記錄目前深度

  • 若 count > max 就取代為最新的 max

型別

解題

Last updated