@@ -329,15 +329,62 @@ <h2 class="section-title" id="header-classes">Classes</h2>
329329 """
330330 Represents a node in a binary tree.
331331
332+ Args:
333+ val: The value stored in the node, or a list to build a tree from.
334+ If a list is provided, builds tree level by level from left to right.
335+ Use None in the list for missing nodes.
336+ left: Reference to the left child node (only used when val is not a list)
337+ right: Reference to the right child node (only used when val is not a list)
338+
332339 Attributes:
333340 val: The value stored in the node
334341 left: Reference to the left child node
335342 right: Reference to the right child node
343+
344+ Example:
345+ >>> # Create a single node
346+ >>> node = BinaryNode(5)
347+ >>>
348+ >>> # Create a tree from a list
349+ >>> root = BinaryNode([1, 2, 3, 4, 5, None, 7])
350+ >>> # Creates:
351+ >>> # 1
352+ >>> # / \
353+ >>> # 2 3
354+ >>> # / \ \
355+ >>> # 4 5 7
336356 """
337357 def __init__(self, val=0, left=None, right=None):
338- self.val = val
339- self.left = left
340- self.right = right
358+ # If val is a list, build tree from it
359+ if isinstance(val, list):
360+ if not val or val[0] is None:
361+ raise ValueError("Cannot create tree from empty list or list starting with None")
362+
363+ self.val = val[0]
364+ self.left = None
365+ self.right = None
366+
367+ queue = [self]
368+ i = 1
369+
370+ while queue and i < len(val):
371+ node = queue.pop(0)
372+
373+ # Add left child
374+ if i < len(val) and val[i] is not None:
375+ node.left = BinaryNode(val[i])
376+ queue.append(node.left)
377+ i += 1
378+
379+ # Add right child
380+ if i < len(val) and val[i] is not None:
381+ node.right = BinaryNode(val[i])
382+ queue.append(node.right)
383+ i += 1
384+ else:
385+ self.val = val
386+ self.left = left
387+ self.right = right
341388
342389 def __str__(self):
343390 return str(self.val)
@@ -346,6 +393,17 @@ <h2 class="section-title" id="header-classes">Classes</h2>
346393 return str(self)</ code > </ pre >
347394</ details >
348395< div class ="desc "> < p > Represents a node in a binary tree.</ p >
396+ < h2 id ="args "> Args</ h2 >
397+ < dl >
398+ < dt > < strong > < code > val</ code > </ strong > </ dt >
399+ < dd > The value stored in the node, or a list to build a tree from.
400+ If a list is provided, builds tree level by level from left to right.
401+ Use None in the list for missing nodes.</ dd >
402+ < dt > < strong > < code > left</ code > </ strong > </ dt >
403+ < dd > Reference to the left child node (only used when val is not a list)</ dd >
404+ < dt > < strong > < code > right</ code > </ strong > </ dt >
405+ < dd > Reference to the right child node (only used when val is not a list)</ dd >
406+ </ dl >
349407< h2 id ="attributes "> Attributes</ h2 >
350408< dl >
351409< dt > < strong > < code > val</ code > </ strong > </ dt >
@@ -354,7 +412,18 @@ <h2 id="attributes">Attributes</h2>
354412< dd > Reference to the left child node</ dd >
355413< dt > < strong > < code > right</ code > </ strong > </ dt >
356414< dd > Reference to the right child node</ dd >
357- </ dl > </ div >
415+ </ dl >
416+ < h2 id ="example "> Example</ h2 >
417+ < pre > < code class ="language-python-repl "> >>> # Create a single node
418+ >>> node = BinaryNode(5)
419+ >>>
420+ >>> # Create a tree from a list
421+ >>> root = BinaryNode([1, 2, 3, 4, 5, None, 7])
422+ >>> # Creates:
423+ >>> # 1
424+ >>> # / >>> # 2 3
425+ >>> # / \ >>> # 4 5 7
426+ </ code > </ pre > </ div >
358427</ dd >
359428</ dl >
360429</ section >
0 commit comments