data_structures.binary_tree.binary_tree_node_sum ================================================ .. py:module:: data_structures.binary_tree.binary_tree_node_sum .. autoapi-nested-parse:: Sum of all nodes in a binary tree. Python implementation: O(n) time complexity - Recurses through :meth:`depth_first_search` with each element. O(n) space complexity - At any point in time maximum number of stack frames that could be in memory is `n` Classes ------- .. autoapisummary:: data_structures.binary_tree.binary_tree_node_sum.BinaryTreeNodeSum data_structures.binary_tree.binary_tree_node_sum.Node Module Contents --------------- .. py:class:: BinaryTreeNodeSum(tree: Node) The below tree looks like this 10 / \ 5 -3 / / \ 12 8 0 >>> tree = Node(10) >>> sum(BinaryTreeNodeSum(tree)) 10 >>> tree.left = Node(5) >>> sum(BinaryTreeNodeSum(tree)) 15 >>> tree.right = Node(-3) >>> sum(BinaryTreeNodeSum(tree)) 12 >>> tree.left.left = Node(12) >>> sum(BinaryTreeNodeSum(tree)) 24 >>> tree.right.left = Node(8) >>> tree.right.right = Node(0) >>> sum(BinaryTreeNodeSum(tree)) 32 .. py:method:: __iter__() -> collections.abc.Iterator[int] .. py:method:: depth_first_search(node: Node | None) -> int .. py:attribute:: tree .. py:class:: Node(value: int) A Node has a value variable and pointers to Nodes to its left and right. .. py:attribute:: left :type: Node | None :value: None .. py:attribute:: right :type: Node | None :value: None .. py:attribute:: value