📌  相关文章
📜  国际空间研究组织 | ISRO CS 2014 |问题 46(1)

📅  最后修改于: 2023-12-03 14:50:47.061000             🧑  作者: Mango

ISRO CS 2014 Question 46

Problem Description

You are given a binary tree and a sum S. The task is to check if there exists a subtree of the given binary tree with sum S.

Input

The input consists of the following:

  • The first line contains an integer N, denoting the number of nodes in the binary tree.
  • The next N lines contain three space-separated values each:
    • The first value is an integer, denoting the value of the node.
    • The second value is an integer, denoting the left child of the node (-1 if no left child exists).
    • The third value is an integer, denoting the right child of the node (-1 if no right child exists).
  • The last line contains an integer S, denoting the sum to be checked.
Output

The output should consist of a single line, containing "YES" if there exists a subtree of the given binary tree with sum S, and "NO" otherwise.

Example

Input:

5
1 2 3
2 4 -1
3 -1 5
4 -1 -1
5 -1 -1
8

Output: "YES"

Explanation

The given binary tree is as follows:

      1
     / \
    2   3
   /     \
  4       5

The subtree with sum 8 is given by the path 2->4->2. Therefore, the output is "YES".

Code Explanation

To solve this problem, we can perform a recursive depth-first search (DFS) on the binary tree. At each node, we can calculate the sum of the values in the subtree rooted at that node. If the sum is equal to S, then we can return "YES". Otherwise, we continue the DFS on the left and right children of the node.

Since we need to return the result from a recursive function, we can use a boolean variable to keep track of whether a subtree with sum S has been found or not. This variable can be initialized to false outside the recursive function, and passed as a reference to the function. If a subtree with sum S is found, we can set this variable to true and return it.

The implementation of this algorithm in Python is given below:

class Node:
    def __init__(self, value, left_child=None, right_child=None):
        self.value = value
        self.left_child = left_child
        self.right_child = right_child

def subtree_sum(root, s, found):
    if root is None:
        return 0
    
    left_sum = subtree_sum(root.left_child, s, found)
    right_sum = subtree_sum(root.right_child, s, found)
    
    total_sum = root.value + left_sum + right_sum
    
    if total_sum == s:
        found[0] = True
        return
    
    return total_sum

root = Node(1, Node(2, Node(4), None), Node(3, None, Node(5)))
s = 8
found = [False]
subtree_sum(root, s, found)

if found[0]:
    print("YES")
else:
    print("NO")

The Node class is used to represent the nodes in the binary tree. The subtree_sum function is the recursive DFS function that calculates the sum of the tree rooted at each node. The found parameter is a list with a single boolean value, which is used to keep track of whether a subtree with sum S has been found. The root parameter is the root node of the binary tree, and the s parameter is the sum to be checked.

A test case is provided at the end of the program to demonstrate how to use the function. The expected output for the test case is "YES".