📌  相关文章
📜  具有相反奇偶校验子树总和的节点总和

📅  最后修改于: 2021-09-04 07:46:56             🧑  作者: Mango

给定一棵二叉树,任务是从给定的树中找到所有这些节点的总和,这些节点的左子树和右子树的总和分别是奇数和偶数或偶数和奇数。

例子:

方法:思想是递归计算左子树右子树的总和,然后检查给定条件。请按照以下步骤解决问题:

  • 将变量ans初始化为0以存储所有此类节点的总和。
  • 在给定的树中执行后序遍历。
  • 找到每个节点的左子树和右子树的总和,并检查总和是否非零,并检查两个总和的总和是否为奇数。如果发现为真,则将当前节点值包含在ans 中
  • 在每次递归调用中返回左子树、右子树的所有节点和当前节点值的总和。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ pprogram for the above approach
#include 
using namespace std;
 
// A binary tree node
struct Node {
 
    int data;
    Node *left, *right;   
};
 
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return(node);
}
    // Stores the desired result
     int mSum;
 
    // Function to find the sum of nodes
    // with subtree sums of opposite parities
     int getSum(Node *root)
    {
        
        // Return 0, if node is NULL
        if (root == NULL)
            return 0;
 
        // Recursively call left and
        // right subtree
        int lSum = getSum(root->left);
        int rSum = getSum(root->right);
 
        // Update mSum, if one subtree
        // sum is even and another is odd
        if (lSum != 0 && rSum != 0)
            if ((lSum + rSum) % 2 != 0)
                mSum += root->data;
 
        // Return the sum of subtree
        return lSum + rSum + root->data;
    }
 
    // Driver Code
    int main()
    {
        // Given number of nodes
        int n = 9;
 
        // Binary tree formation
       struct Node *root = newNode(11);
        root->left =  newNode(23);
        root->right =  newNode(44);
        root->left->left =  newNode(13);
        root->left->right =  newNode(9);
        root->right->left =  newNode(22);
        root->right->right =  newNode(7);
        root->right->left->left =  newNode(6);
        root->right->left->right = newNode(15);
 
        // 11
        //    /     \
        // 23       44
        //  /  \     /   \
        // 13   9   22     7
        //         / \
        // 6   15
 
        mSum = 0;
        getSum(root);
 
        // Print the sum
        cout<<(mSum);
    }
 
// This code is contributed by 29AjayKumar


Java
// Java program for the above approach
 
import java.util.*;
import java.lang.*;
 
// A binary tree node
class Node {
 
    int data;
    Node left, right;
 
    // Constructor
    Node(int item)
    {
        data = item;
        left = right = null;
    }
}
 
// Binary Tree Class
class BinaryTree {
 
    // Stores the desired result
    static int mSum;
 
    Node root;
 
    // Function to find the sum of nodes
    // with subtree sums of opposite parities
    static int getSum(Node root)
    {
        // Return 0, if node is null
        if (root == null)
            return 0;
 
        // Recursively call left and
        // right subtree
        int lSum = getSum(root.left);
        int rSum = getSum(root.right);
 
        // Update mSum, if one subtree
        // sum is even and another is odd
        if (lSum != 0 && rSum != 0)
            if ((lSum + rSum) % 2 != 0)
                mSum += root.data;
 
        // Return the sum of subtree
        return lSum + rSum + root.data;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given number of nodes
        int n = 9;
 
        BinaryTree tree = new BinaryTree();
 
        // Binary tree formation
        tree.root = new Node(11);
        tree.root.left = new Node(23);
        tree.root.right = new Node(44);
        tree.root.left.left = new Node(13);
        tree.root.left.right = new Node(9);
        tree.root.right.left = new Node(22);
        tree.root.right.right = new Node(7);
        tree.root.right.left.left = new Node(6);
        tree.root.right.left.right = new Node(15);
 
        // 11
        //    /     \
        // 23       44
        //  /  \     /   \
        // 13   9   22     7
        //         / \
        // 6   15
 
        mSum = 0;
 
        getSum(tree.root);
 
        // Print the sum
        System.out.println(mSum);
    }
}


Python3
# Python3 program for the above approach
 
# A binary tree node
class Node:
     
    def __init__(self, x):
         
        self.data = x
        self.left = None
        self.right = None
 
# Stores the desired result
mSum = 0
 
# Function to find the sum of nodes
# with subtree sums of opposite parities
def getSum(root):
     
    global mSum
     
    # Return 0, if node is None
    if (root == None):
        return 0
 
    # Recursively call left and
    # right subtree
    lSum = getSum(root.left)
    rSum = getSum(root.right)
 
    # Update mSum, if one subtree
    # sum is even and another is odd
    if (lSum != 0 and rSum != 0):
        if ((lSum + rSum) % 2 != 0):
            mSum += root.data
 
    # Return the sum of subtree
    return lSum + rSum + root.data
 
# Driver Code
if __name__ == '__main__':
     
    # Given number of nodes
    n = 9
 
    # Binary tree formation
    root = Node(11)
    root.left = Node(23)
    root.right = Node(44)
    root.left.left = Node(13)
    root.left.right = Node(9)
    root.right.left = Node(22)
    root.right.right = Node(7)
    root.right.left.left = Node(6)
    root.right.left.right = Node(15)
 
    #     11
    #   /     \
    #  23       44
    # /  \     /   \
    #13   9   22     7
    #        / \
    #       6   15
 
    mSum = 0
 
    getSum(root)
 
    # Print the sum
    print(mSum)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
// A binary tree node
public class Node
{
    public int data;
    public Node left, right;
     
    // Constructor
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
 
// Binary Tree Class
class BinaryTree{
 
// Stores the desired result
static int mSum;
 
Node root;
 
// Function to find the sum of nodes
// with subtree sums of opposite parities
static int getSum(Node root)
{
     
    // Return 0, if node is null
    if (root == null)
        return 0;
 
    // Recursively call left and
    // right subtree
    int lSum = getSum(root.left);
    int rSum = getSum(root.right);
 
    // Update mSum, if one subtree
    // sum is even and another is odd
    if (lSum != 0 && rSum != 0)
        if ((lSum + rSum) % 2 != 0)
            mSum += root.data;
 
    // Return the sum of subtree
    return lSum + rSum + root.data;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number of nodes
    //int n = 9;
 
    BinaryTree tree = new BinaryTree();
 
    // Binary tree formation
    tree.root = new Node(11);
    tree.root.left = new Node(23);
    tree.root.right = new Node(44);
    tree.root.left.left = new Node(13);
    tree.root.left.right = new Node(9);
    tree.root.right.left = new Node(22);
    tree.root.right.right = new Node(7);
    tree.root.right.left.left = new Node(6);
    tree.root.right.left.right = new Node(15);
 
    //       11
    //    /     \
    //   23       44
    //  /  \     /   \
    // 13   9   22     7
    //         / \
    //        6   15
    mSum = 0;
 
    getSum(tree.root);
 
    // Print the sum
    Console.WriteLine(mSum);
}
}
 
// This code is contributed by Amit Katiyar


输出:
33

时间复杂度: O(N),其中 N 是二叉树中的节点数。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live