Skip to content

104. Maximum Depth of Binary Tree#20

Open
miyataka wants to merge 1 commit into
mainfrom
104_MaximumDepthOfBinaryTree
Open

104. Maximum Depth of Binary Tree#20
miyataka wants to merge 1 commit into
mainfrom
104_MaximumDepthOfBinaryTree

Conversation

@miyataka

Copy link
Copy Markdown
Owner

# 見積もり
- 入力サイズ: 0 <= ノード数 <= 10^4
- 時間計算量: O(N)(全ノードをちょうど1回ずつ処理)
- 実行時間の見積もり: 10^4 / 10^8 = 10^-4 s = 0.1ms

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10^8がGoの実行速度であることも明示したほうが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たしかに.
何度も書いてますが,書かなくても自明と思っているのは自分だけですね.

ありがとうございます

return max(countDepth(node.Left), countDepth(node.Right)) + 1
}
```
- 短くはなったが,まだ自然には感じられない

@h-masder h-masder Jun 23, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0
再帰は、この辺りを見るとよいと思います。

それはそれとして、言語化してみるとこんな感じです。
・木構造の各ノードには人が立っており、ルートがボス、その下に部下、さらにその下に部下の部下、、、と続きます。この解答では、末端は葉ノードではなく、葉のさらに下にぶら下がっている nil です(nilがぶら下がっているいうのはあくまでイメージです)。

  1. nilは上司に「深さ0です」と報告。
  2. 葉は上司に「深さ1です」と報告。
  3. それ以外は、左右両方の部下に「最大深さを調べて」と依頼。返ってきた値を比べ、 深い方に1を足して上司に報告。

というイメージです。

@h-masder h-masder Jun 23, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この解答では、で末端は葉ノードではなく、葉のさらに下にいる nil です。

一方で最初の解答は、末端は葉ノードな感じがしました。

  1. 葉は上司に「深さ1です」と報告。
  2. それ以外は、左右両方の部下がnilでないときは「最大深さを調べて」と依頼。返ってきた値を比べ、 深い方に1を足して上司に報告。
    というイメージです

そう考えたとき、最初のコードにおけるnilの条件分岐は何かというと、これはroot=nilに対する処理ですね。ですので、最初のコードは以下のように書くほうが自然な気がします。

func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    maxDepth := 0
    maxDepth = countDepth(root)
    return maxDepth
}

func countDepth(node *TreeNode) int {
    if node.Left == nil && node.Right == nil {
        return 1
    }
    leftDepth := 0
    rightDepth := 0
    if node.Left != nil {
        leftDepth = countDepth(node.Left)
    }
    if node.Right != nil {
        rightDepth = countDepth(node.Right)
    }
    return max(leftDepth, rightDepth) + 1
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど,root以外は,すでにnilチェックを呼び出し前に行っているのでたしかにそれも通りますね.

このへんが統一されていないのは,たしかに明確にイメージがついてなかったから,かもしれません.

}

return max(maxDepth(node.Left), maxDepth(node.Right)) + 1
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node, node.Left, node.Rightはそれぞれ、root, root.Left, root.Rightですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ああ,これは動作したコードをちゃんとコピペせず,適当に再現してしまったためですね.
大変失礼しました.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants