Skip to content

Balanced Binary Tree#95

Open
tom4649 wants to merge 2 commits into
mainfrom
110.Balanced-Binary-Tree
Open

Balanced Binary Tree#95
tom4649 wants to merge 2 commits into
mainfrom
110.Balanced-Binary-Tree

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented May 16, 2026

height_right = node_to_height.get(node.right)

if height_left is None or height_right is None:
stack.append(node)
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 を追加することで、子を調べたあとで戻ってきて調べることができるという点が、ややパズルに感じました。個人的には acceptable だと感じました。

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 May 19, 2026

Choose a reason for hiding this comment

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

再帰で書く方が自然だと思います。

if node is None:
return 0
left_h = height_or_unbalanced(node.left)
if left_h == -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.

-1 がマジックナンバーになってしまっているのが気になりました。 UNBALANCED 等定数に置いたほうが、理解しやすくなると思います。

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