Skip to content

Commit 8fed83a

Browse files
authored
Merge pull request #2742 from keon/fix/treenode-dedup-falsy-zero
Fix falsy-zero bug in BST validation, deduplicate TreeNode
2 parents 9f1446c + 41abbfc commit 8fed83a

4 files changed

Lines changed: 10 additions & 42 deletions

File tree

algorithms/tree/bst_array_to_bst.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
convert it to a height balanced BST.
44
"""
55

6-
7-
class TreeNode:
8-
def __init__(self, x):
9-
self.val = x
10-
self.left = None
11-
self.right = None
6+
from algorithms.common.tree_node import TreeNode
127

138

149
def array_to_bst(nums):

algorithms/tree/bst_serialize_deserialize.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
class TreeNode:
2-
def __init__(self, x):
3-
self.val = x
4-
self.left = None
5-
self.right = None
1+
from algorithms.common.tree_node import TreeNode
62

73

84
def serialize(root):

algorithms/tree/bst_validate_bst.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@
1212
"""
1313
# ===============================================================================
1414

15-
16-
# Tree class definition
17-
class TreeNode:
18-
def __init__(self, value):
19-
20-
self.val = value
21-
self.left = None
22-
self.right = None
15+
from algorithms.common.tree_node import TreeNode
2316

2417

2518
# Function to validate if a binary tree is a BST
@@ -46,24 +39,24 @@ def validate_bst(node):
4639
if not valid_left or not valid_right:
4740
return (
4841
False,
49-
minn_left if minn_left else node.val,
50-
maxx_right if maxx_right else node.val,
42+
minn_left if minn_left is not None else node.val,
43+
maxx_right if maxx_right is not None else node.val,
5144
)
5245

5346
# Check the current node's value against the max of the left subtree
5447
if maxx_left is not None and maxx_left > node.val:
5548
return (
5649
False,
57-
minn_left if minn_left else node.val,
58-
maxx_right if maxx_right else node.val,
50+
minn_left if minn_left is not None else node.val,
51+
maxx_right if maxx_right is not None else node.val,
5952
)
6053

6154
# Check the current node's value against the min of the right subtree
6255
if minn_right is not None and minn_right < node.val:
6356
return (
6457
False,
65-
minn_left if minn_left else node.val,
66-
maxx_right if maxx_right else node.val,
58+
minn_left if minn_left is not None else node.val,
59+
maxx_right if maxx_right is not None else node.val,
6760
)
6861

6962
# If all checks pass, the tree/subtree is a valid BST

algorithms/tree/construct_tree_postorder_preorder.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,7 @@
1414

1515
from __future__ import annotations
1616

17-
18-
class TreeNode:
19-
"""A node in a binary tree.
20-
21-
Args:
22-
val: The value stored in this node.
23-
left: The left child node.
24-
right: The right child node.
25-
"""
26-
27-
def __init__(
28-
self, val: int, left: TreeNode | None = None, right: TreeNode | None = None
29-
) -> None:
30-
self.val = val
31-
self.left = left
32-
self.right = right
33-
17+
from algorithms.common.tree_node import TreeNode
3418

3519
pre_index = 0
3620

0 commit comments

Comments
 (0)