原文:

给定一棵二叉树。任务是打印二叉树中每个节点的设置位数。

其思想是使用任意遍历给定的二叉树,对于每个节点计算设置位数并打印出来。

:也可以使用 c 中的 __builtin_popcount() 函数直接对整数中的设置位数进行计数。

下面是上述方法的实现:

c

// cpp program to print the number of set bits
// in each node of the binary tree
#include 
using namespace std;
// binary tree node
struct node {
    int data;
    struct node *left, *right;
};
// utility function that allocates a new node
node* newnode(int data)
{
    node* node = new node;
    node->data = data;
    node->left = node->right = null;
    return (node);
}
// function to print the number of set bits
// in each node of the binary tree
void printsetbit(node* root)
{
    if (root == null)
        return;
    // print the number of set bits of
    // current node using __builtin_popcount()
    cout << "set bits in node " << root->data << " = " <<
                  __builtin_popcount(root->data) << "\n";
    // traverse left subtree
    printsetbit(root->left);
    // traverse right subtree
    printsetbit(root->right);
}
// driver code
int main()
{
    node* root = newnode(16);
    root->left = newnode(13);
    root->left->left = newnode(14);
    root->left->right = newnode(12);
    root->right = newnode(11);
    root->right->left = newnode(10);
    root->right->right = newnode(16);
    printsetbit(root);
    return 0;
}

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

// java program to print the number of set bits
// in each node of the binary tree
import java.util.*;
class gfg
{
// binary tree node
static class node
{
    int data;
    node left, right;
};
// utility function that allocates a new node
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
// function to print the number of set bits
// in each node of the binary tree
static void printsetbit(node root)
{
    if (root == null)
        return;
    // print the number of set bits of
    // current node using __builtin_popcount()
    system.out.print("set bits in node "   root.data   " = "  
                      integer.bitcount(root.data)   "\n");
    // traverse left subtree
    printsetbit(root.left);
    // traverse right subtree
    printsetbit(root.right);
}
// driver code
public static void main(string[] args)
{
    node root = newnode(16);
    root.left = newnode(13);
    root.left.left = newnode(14);
    root.left.right = newnode(12);
    root.right = newnode(11);
    root.right.left = newnode(10);
    root.right.right = newnode(16);
    printsetbit(root);
}
}
// this code is contributed by princiraj1992

python 3

# python3 program to print the number of set bits
# in each node of the binary tree
# binary tree node
class node:
    def __init__(self):
        self.data = none
        self.left = none
        self.right = none
# utility function that allocates a new node
def newnode(data):
    node = node()
    node.data = data
    node.left = none
    node.right = none
    return node
# function to print the number of set bits
# in each node of the binary tree
def printsetbit(root: node):
    if root is none:
        return
    # print the number of set bits of
    # current node using count()
    print("set bits in node %d = %d" %
         (root.data, bin(root.data).count('1')))
    # traverse left subtree
    printsetbit(root.left)
    # traverse right subtree
    printsetbit(root.right)
# driver code
if __name__ == "__main__":
    root = newnode(16)
    root.left = newnode(13)
    root.left.left = newnode(14)
    root.left.right = newnode(12)
    root.right = newnode(11)
    root.right.left = newnode(10)
    root.right.right = newnode(16)
    printsetbit(root)
# this code is contributed by
# sanjeev2552

c

// c# program to print the number of set bits
// in each node of the binary tree
using system;
class gfg
{
// binary tree node
public class node
{
    public int data;
    public node left, right;
};
// utility function that allocates a new node
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
// function to print the number of set bits
// in each node of the binary tree
static void printsetbit(node root)
{
    if (root == null)
        return;
    // print the number of set bits of
    // current node using __builtin_popcount()
    console.write("set bits in node "   root.data  
                  " = "   bitcount(root.data)   "\n");
    // traverse left subtree
    printsetbit(root.left);
    // traverse right subtree
    printsetbit(root.right);
}
static int bitcount(int x)
{
    int setbits = 0;
    while (x != 0)
    {
        x = x & (x - 1);
        setbits  ;
    }
    return setbits;
}
// driver code
public static void main(string[] args)
{
    node root = newnode(16);
    root.left = newnode(13);
    root.left.left = newnode(14);
    root.left.right = newnode(12);
    root.right = newnode(11);
    root.right.left = newnode(10);
    root.right.right = newnode(16);
    printsetbit(root);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

set bits in node 16 = 1
set bits in node 13 = 3
set bits in node 14 = 3
set bits in node 12 = 2
set bits in node 11 = 3
set bits in node 10 = 2
set bits in node 16 = 1