在给定的二叉树中找到具有最大 setbit 计数的级别
给定具有N个节点的二叉树,任务是找到具有最大 setbit 数的级别。
注意:如果两个级别具有相同数量的 setbit,则打印具有较少节点的级别。如果节点相等,则从上到下打印第一级
例子:
Input:
2
/ \
5 3
/ \
6 1
Output: 2
Explanation: Level 1 has only one setbit => 2 (010).
Level 2 has 4 setbits. => 5 (101) + 3 (011).
Level 3 has 3 setbits. => 6 (110) +1 (001).
Input:
2
/ \
5 3
/ \ \
6 1 8
Output: 2
方法:可以使用水平顺序遍历本身来解决问题。根据问题中的给定条件,找出每个级别中的 setbit 数量以及具有最大 setbit 数量的级别。请按照以下步骤操作:
- 使用级别顺序遍历和对于每个级别:
- 查找每个级别中的 setbit 总数。
- 更新一个级别中的最大 setbits 和具有最大 setbit 数量的级别。
- 返回具有最大设置位的级别。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Structure of a binary tree node
struct Node {
int data;
struct Node *left, *right;
};
// Function to count no of set bit
int countSetBit(int x)
{
int c = 0;
while (x) {
int l = x % 10;
if (x & 1)
c++;
x /= 2;
}
return c;
}
// Function to convert tree element
// by count of set bit they have
void convert(Node* root)
{
if (!root)
return;
root->data = countSetBit(root->data);
convert(root->left);
convert(root->right);
}
// Function to get level with max set bit
int printLevel(Node* root)
{
// Base Case
if (root == NULL)
return 0;
// Replace tree elements by
// count of set bits they contain
convert(root);
// Create an empty queue
queue q;
int currLevel = 0, ma = INT_MIN;
int prev = 0, ans = 0;
// Enqueue Root and initialize height
q.push(root);
// Loop to implement level order traversal
while (q.empty() == false) {
// Print front of queue and
// remove it from queue
int size = q.size();
currLevel++;
int totalSet = 0, nodeCount = 0;
while (size--) {
Node* node = q.front();
// Add all the set bit
// in the current level
totalSet += node->data;
q.pop();
// Enqueue left child
if (node->left != NULL)
q.push(node->left);
// Enqueue right child
if (node->right != NULL)
q.push(node->right);
// Count current level node
nodeCount++;
}
// Update the ans when needed
if (ma < totalSet) {
ma = totalSet;
ans = currLevel;
}
// If two level have same set bit
// one with less node become ans
else if (ma == totalSet && prev > nodeCount) {
ma = totalSet;
ans = currLevel;
prev = nodeCount;
}
// Assign prev =
// current level node count
// We can use it for further levels
// When 2 level have
// same set bit count
// print level with less node
prev = nodeCount;
}
return ans;
}
// Utility function to create new tree node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Driver program
int main()
{
// Binary tree as shown in example
Node* root = newNode(2);
root->left = newNode(5);
root->right = newNode(3);
root->left->left = newNode(6);
root->left->right = newNode(1);
root->right->right = newNode(8);
// Function call
cout << printLevel(root) << endl;
return 0;
}
Python3
# Python code for the above approach
# Structure of a binary Tree node
import sys
class Node:
def __init__(self,d):
self.data = d
self.left = None
self.right = None
# Function to count no of set bit
def countSetBit(x):
c = 0
while (x):
l = x % 10
if (x & 1):
c += 1
x = (x // 2)
return c
# Function to convert tree element
# by count of set bit they have
def convert(root):
if (root == None):
return
root.data = countSetBit(root.data)
convert(root.left)
convert(root.right)
# Function to get level with max set bit
def printLevel(root):
# Base Case
if (root == None):
return 0
# Replace tree elements by
# count of set bits they contain
convert(root)
# Create an empty queue
q = []
currLevel,ma = 0, -sys.maxsize - 1
prev,ans = 0,0
# Enqueue Root and initialize height
q.append(root)
# Loop to implement level order traversal
while (len(q) != 0):
# Print front of queue and
# remove it from queue
size = len(q)
currLevel += 1
totalSet,nodeCount = 0,0
while (size):
node = q[0]
q = q[1:]
# Add all the set bit
# in the current level
totalSet += node.data
# Enqueue left child
if (node.left != None):
q.append(node.left)
# Enqueue right child
if (node.right != None):
q.append(node.right)
# Count current level node
nodeCount += 1
size -= 1
# Update the ans when needed
if (ma < totalSet):
ma = totalSet
ans = currLevel
# If two level have same set bit
# one with less node become ans
elif (ma == totalSet and prev > nodeCount):
ma = totalSet
ans = currLevel
prev = nodeCount
# Assign prev =
# current level node count
# We can use it for further levels
# When 2 level have
# same set bit count
# print level with less node
prev = nodeCount
return ans
# Driver program
# Binary tree as shown in example
root = Node(2)
root.left = Node(5)
root.right = Node(3)
root.left.left = Node(6)
root.left.right = Node(1)
root.right.right = Node(8)
# Function call
print(printLevel(root))
# This code is contributed by shinjanpatra
Javascript
输出
2
时间复杂度: (N * D) 其中 D 是元素的位数
辅助空间: O(N)