原文:

给定一棵二叉树,求所有叶节点的乘积。 示例:

input : 
        1
      /   \
     2     3
    / \   / \
   4   5 6   7
          \
           8
output :
product = 4 * 5 * 8 * 7 = 1120

其思想是以任何方式遍历树,并检查该节点是否是叶节点。如果节点是叶节点,将节点数据乘以一个变量 prod ,用于存储叶节点的乘积。 以下是上述办法的实施情况。

c

// cpp program to find product of
// all leaf nodes of binary tree
#include 
using namespace std;
// struct binary tree node
struct node {
    int data;
    node *left, *right;
};
// return new node
node* newnode(int data)
{
    node* temp = new node();
    temp->data = data;
    temp->left = temp->right = null;
    return temp;
}
// utility function which calculates
// product of all leaf nodes
void leafproduct(node* root, int& prod)
{
    if (!root)
        return;
    // product root data to prod if
    // root is a leaf node
    if (!root->left && !root->right)
        prod *= root->data;
    // propagate recursively in left
    // and right subtree
    leafproduct(root->left, prod);
    leafproduct(root->right, prod);
}
// driver program
int main()
{
    // construct binary tree
    node* root = newnode(1);
    root->left = newnode(2);
    root->left->left = newnode(4);
    root->left->right = newnode(5);
    root->right = newnode(3);
    root->right->right = newnode(7);
    root->right->left = newnode(6);
    root->right->left->right = newnode(8);
    // variable to store product of leaf nodes
    int prod = 1;
    leafproduct(root, prod);
    cout << prod << endl;
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find product of
// all leaf nodes of binary tree
class gfg
{
// struct binary tree node
static class node
{
    int data;
    node left, right;
};
// return new node
static node newnode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
// product
static int prod = 1;
// utility function which calculates
// product of all leaf nodes
static void leafproduct(node root )
{
    if (root == null)
        return;
    // product root data to prod if
    // root is a leaf node
    if (root.left == null && root.right == null)
        prod *= root.data;
    // propagate recursively in left
    // and right subtree
    leafproduct(root.left);
    leafproduct(root.right);
}
// driver program
public static void main(string args[])
{
    // construct binary tree
    node root = newnode(1);
    root.left = newnode(2);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    root.right = newnode(3);
    root.right.right = newnode(7);
    root.right.left = newnode(6);
    root.right.left.right = newnode(8);
    // variable to store product of leaf nodes
    prod = 1;
    leafproduct(root);
    system.out.println(prod );
}
}
// this code is contributed by arnab kundu

计算机编程语言

# python program to find product of
# all leaf nodes of binary tree
# node class
class node:
    # function to initialise the node object
    def __init__(self, data):
        self.data = data # assign data
        self.next =none
# return new node
def newnode( data) :
    temp = node(0)
    temp.data = data
    temp.left = temp.right = none
    return temp
# product
prod = 1
# utility function which calculates
# product of all leaf nodes
def leafproduct( root ) :
    global prod
    if (root == none) :
        return
    # product root data to prod if
    # root is a leaf node
    if (root.left == none and root.right == none):
        prod *= root.data
    # propagate recursively in left
    # and right subtree
    leafproduct(root.left)
    leafproduct(root.right)
# driver program
# construct binary tree
root = newnode(1)
root.left = newnode(2)
root.left.left = newnode(4)
root.left.right = newnode(5)
root.right = newnode(3)
root.right.right = newnode(7)
root.right.left = newnode(6)
root.right.left.right = newnode(8)
# variable to store product of leaf nodes
prod = 1
leafproduct(root)
print(prod )
# this code is contributed by arnab kundu

c

// c# program to find product of
// all leaf nodes of binary tree
using system;
class gfg
{
// struct binary tree node
public class node
{
    public int data;
    public node left, right;
};
// return new node
static node newnode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
// product
static int prod = 1;
// utility function which calculates
// product of all leaf nodes
static void leafproduct(node root )
{
    if (root == null)
        return;
    // product root data to prod if
    // root is a leaf node
    if (root.left == null && root.right == null)
        prod *= root.data;
    // propagate recursively in left
    // and right subtree
    leafproduct(root.left);
    leafproduct(root.right);
}
// driver code
public static void main(string []args)
{
    // construct binary tree
    node root = newnode(1);
    root.left = newnode(2);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    root.right = newnode(3);
    root.right.right = newnode(7);
    root.right.left = newnode(6);
    root.right.left.right = newnode(8);
    // variable to store product of leaf nodes
    prod = 1;
    leafproduct(root);
    console.writeline(prod );
}
}
// this code has been contributed by 29ajaykumar

java 描述语言


output: 

1120

时间复杂度: o(n),其中 n 为树中节点总数。