Skip to content
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 26, 2025
commit ee149d6d6695c32aecb4168d1edc6fcc1d09f34f
44 changes: 34 additions & 10 deletions data_structures/binary_tree/build_tree_from_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Doctest examples are included for verification.
"""

from __future__ import annotations # for forward references
from typing import Optional

Check failure on line 13 in data_structures/binary_tree/build_tree_from_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

data_structures/binary_tree/build_tree_from_traversal.py:13:20: F401 `typing.Optional` imported but unused

Check failure on line 13 in data_structures/binary_tree/build_tree_from_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/binary_tree/build_tree_from_traversal.py:12:1: I001 Import block is un-sorted or un-formatted

Check failure on line 13 in data_structures/binary_tree/build_tree_from_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

data_structures/binary_tree/build_tree_from_traversal.py:13:20: F401 `typing.Optional` imported but unused

Check failure on line 13 in data_structures/binary_tree/build_tree_from_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/binary_tree/build_tree_from_traversal.py:12:1: I001 Import block is un-sorted or un-formatted


class Node:
Expand Down Expand Up @@ -50,12 +50,22 @@
nums_left = in_root_index - in_start

root.left = _build_tree_from_preorder(
preorder, pre_start + 1, pre_start + nums_left,
inorder, in_start, in_root_index - 1, in_map
preorder,
pre_start + 1,
pre_start + nums_left,
inorder,
in_start,
in_root_index - 1,
in_map,
)
root.right = _build_tree_from_preorder(
preorder, pre_start + nums_left + 1, pre_end,
inorder, in_root_index + 1, in_end, in_map
preorder,
pre_start + nums_left + 1,
pre_end,
inorder,
in_root_index + 1,
in_end,
in_map,
)

return root
Expand All @@ -64,7 +74,9 @@
def build_tree_from_preorder(inorder: list[int], preorder: list[int]) -> Node | None:

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/build_tree_from_traversal.py, please provide doctest for the function build_tree_from_preorder

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/build_tree_from_traversal.py, please provide doctest for the function build_tree_from_preorder

"""Build binary tree from preorder and inorder sequences."""
in_map = {val: idx for idx, val in enumerate(inorder)}
return _build_tree_from_preorder(preorder, 0, len(preorder) - 1, inorder, 0, len(inorder) - 1, in_map)
return _build_tree_from_preorder(
preorder, 0, len(preorder) - 1, inorder, 0, len(inorder) - 1, in_map
)


def _build_tree_from_postorder(
Expand Down Expand Up @@ -99,12 +111,22 @@
nums_left = in_root_index - in_start

root.left = _build_tree_from_postorder(
postorder, post_start, post_start + nums_left - 1,
inorder, in_start, in_root_index - 1, in_map
postorder,
post_start,
post_start + nums_left - 1,
inorder,
in_start,
in_root_index - 1,
in_map,
)
root.right = _build_tree_from_postorder(
postorder, post_start + nums_left, post_end - 1,
inorder, in_root_index + 1, in_end, in_map
postorder,
post_start + nums_left,
post_end - 1,
inorder,
in_root_index + 1,
in_end,
in_map,
)

return root
Expand All @@ -113,7 +135,9 @@
def build_tree_from_postorder(inorder: list[int], postorder: list[int]) -> Node | None:

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/build_tree_from_traversal.py, please provide doctest for the function build_tree_from_postorder

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/build_tree_from_traversal.py, please provide doctest for the function build_tree_from_postorder

"""Build binary tree from postorder and inorder sequences."""
in_map = {val: idx for idx, val in enumerate(inorder)}
return _build_tree_from_postorder(postorder, 0, len(postorder) - 1, inorder, 0, len(inorder) - 1, in_map)
return _build_tree_from_postorder(
postorder, 0, len(postorder) - 1, inorder, 0, len(inorder) - 1, in_map
)


# Optional example usage
Expand Down
Loading