原文:

给定一个二叉树。任务是查找并打印树中所有内部节点(非叶节点)的乘积和总和。

在上面的树中,只有两个节点 1 和 2 是非叶节点。 因此,非叶节点的积= 1 * 2 = 2。 非叶节点之和= 1 2 =3。 例:

input :
        1
      /   \
     2     3
    / \   / \
   4   5 6   7
          \
           8
output : product  = 36, sum = 12
non-leaf nodes are: 1, 2, 3, 6 

方法:想法是以任何方式遍历树,并检查当前节点是否是非叶节点。取两个变量分别存储非叶节点的积和。如果当前节点是非叶节点,则将该节点的数据乘以用于存储非叶节点乘积的变量乘积,并将该节点的数据加到用于存储非叶节点总和的变量和上。 以下是上述思路的实现:

c

// cpp program to find product and sum of
// non-leaf nodes in a binary tree
#include 
using namespace std;
/* a binary tree node has data, pointer to
left child and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* 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);
}
// computes the product of non-leaf
// nodes in a tree
void findproductsum(struct node* root, int& prod, int& sum)
{
    // base cases
    if (root == null || (root->left == null
                            && root->right == null))
        return;
    // if current node is non-leaf,
    // calculate product and sum
    if (root->left != null || root->right != null)
    {
        prod *= root->data;
        sum  = root->data;
    }
    // if root is not null and its one of its
    // child is also not null
    findproductsum(root->left, prod, sum);
    findproductsum(root->right, prod, sum);
}
// driver code
int main()
{  
    // binary tree
    struct node* root = newnode(1);
    root->left = newnode(2);
    root->right = newnode(3);
    root->left->left = newnode(4);
    root->left->right = newnode(5);
    int prod = 1;
    int sum = 0;
    findproductsum(root, prod, sum);
    cout <<"product = "<// java program to find product and sum of
// non-leaf nodes in a binary tree
class gfg
{
/* a binary tree node has data, pointer to
left child and a pointer to right child */
static class node
{
    int data;
    node left;
    node right;
};
/* helper function that allocates a new node with the
given data and null left and right pointers. */
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
//int class
static class int
{
    int a;
}
// computes the product of non-leaf
// nodes in a tree
static void findproductsum(node root, int prod, int sum)
{
    // base cases
    if (root == null || (root.left == null
                            && root.right == null))
        return;
    // if current node is non-leaf,
    // calculate product and sum
    if (root.left != null || root.right != null)
    {
        prod.a *= root.data;
        sum.a  = root.data;
    }
    // if root is not null and its one of its
    // child is also not null
    findproductsum(root.left, prod, sum);
    findproductsum(root.right, prod, sum);
}
// driver code
public static void main(string args[])
{
    // binary tree
    node root = newnode(1);
    root.left = newnode(2);
    root.right = newnode(3);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    int prod = new int();prod.a = 1;
    int sum = new int(); sum.a = 0;
    findproductsum(root, prod, sum);
    system.out.print("product = "   prod.a   " , sum = "   sum.a);
}
}
// this code is contributed by arnab kundu

python 3

# python3 program to find product and sum
# of non-leaf nodes in a binary tree
# helper function that allocates a new
# node with the given data and none
# left and right poers.                                
class newnode:
    # construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = none
        self.right = none
# computes the product of non-leaf
# nodes in a tree
class new:
    def findproductsum(sf,root) :
        # base cases
        if (root == none or (root.left == none and
                             root.right == none)) :
            return
        # if current node is non-leaf,
        # calculate product and sum
        if (root.left != none or
            root.right != none) :
            sf.prod *= root.data
            sf.sum  = root.data
        # if root is not none and its one
        # of its child is also not none
        sf.findproductsum(root.left)
        sf.findproductsum(root.right)
    def main(sf):
        root = newnode(1)
        root.left = newnode(2)
        root.right = newnode(3)
        root.left.left = newnode(4)
        root.left.right = newnode(5)
        sf.prod = 1
        sf.sum = 0
        sf.findproductsum(root)
        print("product =", sf.prod,
              ", sum =", sf.sum)
# driver code
if __name__ == '__main__':
    x = new()
    x.main()
# this code is contributed by
# shubham singh(shubhamsingh10)

c

// c# program to find product and sum of
// non-leaf nodes in a binary tree
using system;
class gfg
{
/* a binary tree node has data, pointer to
left child and a pointer to right child */
public class node
{
    public int data;
    public node left;
    public node right;
};
/* helper function that allocates a new node with the
given data and null left and right pointers. */
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
// int class
public class int
{
    public int a;
}
// computes the product of non-leaf
// nodes in a tree
static void findproductsum(node root, int prod, int sum)
{
    // base cases
    if (root == null || (root.left == null
                            && root.right == null))
        return;
    // if current node is non-leaf,
    // calculate product and sum
    if (root.left != null || root.right != null)
    {
        prod.a *= root.data;
        sum.a  = root.data;
    }
    // if root is not null and its one of its
    // child is also not null
    findproductsum(root.left, prod, sum);
    findproductsum(root.right, prod, sum);
}
// driver code
public static void main(string []args)
{
    // binary tree
    node root = newnode(1);
    root.left = newnode(2);
    root.right = newnode(3);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    int prod = new int();prod.a = 1;
    int sum = new int(); sum.a = 0;
    findproductsum(root, prod, sum);
    console.write("product = "   prod.a   " , sum = "   sum.a);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

product = 2 , sum = 3