原文:

给定一个整数“k”和一个字符串格式的二叉树。树的每个节点的值都在 0 到 9 之间。我们需要从根开始寻找 k 级元素的乘积。根在 0 级。 注:树以下列形式给出:(节点值(左子树)(右子树)) 例:

input : tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" 
        k = 2
output : 72
its tree representation is shown below
elements at level k = 2 are 6, 4, 1, 3
sum of the digits of these elements = 6 * 4 * 1 * 3 = 72 
input : tree = "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))" 
        k = 3
output : 15
elements at level k = 3 are 5, 1 and 3
sum of digits of these elements = 5 * 1 * 3 = 15

进场:

1\. input 'tree' in string format and level k
2\. initialize level = -1 and product = 1
3\. for each character 'ch' in 'tree'
   3.1  if ch == '(' then
        --> level  
   3.2  else if ch == ')' then
        --> level--
   3.3  else
        if level == k then
           product = product * (ch-'0')
4\. print product

c

// c   implementation to find product of
// digits of elements at k-th level
#include 
using namespace std;
// function to find product of digits
// of elements at k-th level
int productatkthlevel(string tree, int k)
{
    int level = -1;
    int product = 1; // initialize result
    int n = tree.length();
    for (int i = 0; i < n; i  ) {
        // increasing level number
        if (tree[i] == '(')
            level  ;
        // decreasing level number
        else if (tree[i] == ')')
            level--;
        else {
            // check if current level is
            // the desired level or not
            if (level == k)
                product *= (tree[i] - '0');
        }
    }
    // required product
    return product;
}
// driver program
int main()
{
    string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))";
    int k = 2;
    cout << productatkthlevel(tree, k);
    return 0;
}

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

// java implementation to find product of
// digits of elements at k-th level
class gfg
{
    // function to find product of digits
    // of elements at k-th level
    static int productatkthlevel(string tree, int k)
    {
        int level = -1;
        // initialize result
        int product = 1;
        int n = tree.length();
        for (int i = 0; i < n; i  )
        {
            // increasing level number
            if (tree.charat(i) == '(')
                level  ;
            // decreasing level number
            else if (tree.charat(i) == ')')
                level--;
            else
            {
                // check if current level is
                // the desired level or not
                if (level == k)
                    product *= (tree.charat(i) - '0');
            }
        }
        // required product
        return product;
    }
    // driver program
    public static void main(string[] args)
    {
        string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))";
        int k = 2;
        system.out.println(productatkthlevel(tree, k));
    }
}
// this code is contributed
// by smitha dinesh semwal.

python 3

# python 3 implementation
# to find product of
# digits of elements
# at k-th level
# function to find
# product of digits
# of elements at
# k-th level
def productatkthlevel(tree, k):
    level = -1
        # initialize result
    product = 1
    n = len(tree)
    for i in range(0, n):
        # increasing level number
        if (tree[i] == '('):
            level =1
        # decreasing level number
        elif (tree[i] == ')'):
            level-=1
        else:
            # check if current level is
            # the desired level or not
            if (level == k):
                product *= (int(tree[i]) - int('0'))
    # required product
    return product
# driver program
tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"
k = 2
print(productatkthlevel(tree, k))
# this code is contributed by
# smitha dinesh semwal

c

// c# implementation to find
// product of digits of
// elements at k-th level
using system;
class gfg
{
    // function to find product
    // of digits of elements
    // at k-th level
    static int productatkthlevel(string tree,
                                 int k)
    {
        int level = -1;
        // initialize result
        int product = 1;
        int n = tree.length;
        for (int i = 0; i < n; i  )
        {
            // increasing
            // level number
            if (tree[i] == '(')
                level  ;
            // decreasing
            // level number
            else if (tree[i] == ')')
                level--;
            else
            {
                // check if current level is
                // the desired level or not
                if (level == k)
                    product *= (tree[i] - '0');
            }
        }
        // required product
        return product;
    }
    // driver code
    static void main()
    {
        string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))";
        int k = 2;
        console.writeline(productatkthlevel(tree, k));
    }
}
// this code is contributed by sam007

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

72

时间复杂度: o(n)

https://youtu.be/y

-耶尔马 xlsbq?list = plqm 7 alhxfyshcxd 7r 1j0ky 9 zg _ gbb 1 dbk〖t0〗]