We are given a binary tree. We need to find the sum of all nodes that have two children.
That means a node that has both left and right child (non-null) is counted.
Steps:
- Traverse the tree (any order: pre-order, in-order, post-order).
- For each node, check if it has both left and right children.
- If yes, add its value to the sum.
- Return the total sum.
Let's define a TreeNode class first (assuming the tree is built with such nodes).
We can do a recursive traversal.
Example:
Let the tree be:
1
/
2 3
/ \
4 5 6
Nodes with two children: node 1 (has 2 and 3) and node 2 (has 4 and 5). So sum = 1+2 = 3.
Note: node 3 has only one child (right child 6) so not counted.
Algorithm:
We can write a helper function that does the traversal and accumulates the sum.
Pseudocode:
function sumOfNodesWithTwoChildren(root):
if root is null: return 0
sum = 0
if root.left is not null and root.right is not null:
sum += root.val
sum += sumOfNodesWithTwoChildren(root.left)
sum += sumOfNodesWithTwoChildren(root.right)
return sum
Alternatively, we can do an iterative traversal (BFS or DFS) but recursion is straightforward.
Let's write the code accordingly.
To solve this problem, we need to find the sum of all nodes in a binary tree that have exactly two children (both left and right child are non-null). We'll traverse the tree and check each node to see if it meets this condition, accumulating the sum of such nodes' values.
Approach
- Tree Traversal: We'll use a depth-first search (DFS) approach, specifically a post-order traversal, where we process the left subtree, then the right subtree, and finally the current node. However, any traversal (pre-order, in-order, post-order) works because we simply check each node independently.
- Check Condition: For each node visited, check if it has both a left and a right child. If yes, add its value to the sum.
- Recursion: Use recursion to traverse the left and right subtrees, accumulating the sum from all符合条件的节点。
Solution Code
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sum_of_nodes_with_two_children(root):
"""
Returns the sum of all nodes that have two children (both left and right are not None).
"""
if root is None:
return 0
total = 0
# Check if current node has two children
if root.left is not None and root.right is not None:
total += root.val
# Recurse on left and right subtrees
total += sum_of_nodes_with_two_children(root.left)
total += sum_of_nodes_with_two_children(root.right)
return total
Explanation
- Base Case: If the node is
None (empty subtree), return 0.
- Condition Check: For the current node, if both
left and right pointers are non-null, the node has two children, so its value is added to total.
- Recursive Calls: The function is called recursively on the left and right subtrees. Their returned sums are added to
total.
- Return: The accumulated
total is returned up the recursion stack.
Example
Consider the tree:
1
/ \
2 3
/ \ \
4 5 6
- Node
1 has both children → add 1.
- Node
2 has both children → add 2.
- Nodes
3, 4, 5, 6 do not have two children (node 3 has only one child, leaves have none).
- Total sum = 1 + 2 = 3.
Complexity Analysis
- Time Complexity: O(n), where n is the number of nodes. Each node is visited exactly once.
- Space Complexity: O(h) for the recursion stack, where h is the height of the tree (worst-case O(n) for a skewed tree, average O(log n) for a balanced tree).
This approach efficiently checks every node once and accumulates the sum of nodes with two children using a simple recursive DFS.