今、LeetCodeというサイトでアルゴリズム問題を解いているのですが、
二分木の高さを求める問題に苦戦しています。
以下問題です。
=====================================================================
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
=====================================================================
考え方をおしえていただけたら幸いです!
よろしくお願いします!
以下追記
調べていたら、このような回答を見つけたのですが、
まったくもってコードの仕組みがわからないです。
ruby
1def max_depth(root) 2 root ? 1 + [max_depth(root.left), max_depth(root.right)].max : 0 3end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/09 07:43