Skip to content

104. Maximum Depth of Binary Tree#21

Open
tarinaihitori wants to merge 1 commit intomainfrom
104-maximum-depth-of-binary-tree
Open

104. Maximum Depth of Binary Tree#21
tarinaihitori wants to merge 1 commit intomainfrom
104-maximum-depth-of-binary-tree

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
tree_nodes = deque([root])
depth = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while内でdepthのインクリメントを忘れています。


BFS。
木がバランスしていても空間計算量が O(n)のため、今回のケースだと DFS のほうがより効率的になる可能性が高い。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BFS はいくつか書き方があるんですが、階層ごとにしたいときには、私は List 2つで作るのが好みです。
Yusan1234/arai60#1 (comment)
https://discord.com/channels/1084280443945353267/1201211204547383386/1219179255615717399

一つの変数に、2つの違う種類のものを入れておいて、その境界を個数で管理している
データの整合性が取れているということは、読んでいる人からすると全部読み終わらないと分からない

下は関係のあるようなないような読み物です。Queue を Stack で代用する話ですね。
https://cp-algorithms.com/data_structures/stack_queue_modification.html

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.

3 participants