📜  最大化二叉树中根到叶路径中设置位的计数

📅  最后修改于: 2021-09-05 08:39:57             🧑  作者: Mango

给定一棵二叉树,任务是找到所有根到叶路径的节点值中设置位的总数,并打印其中的最大值。

例子:

方法
请按照以下步骤解决问题:

  • 从根节点开始递归遍历每个节点
  • 计算当前节点的值中设置的位数。
  • 更新设置位的最大计数(存储在变量中,比如maxm )。
  • 遍历它的左右子树。
  • 遍历完树的所有节点后,打印maxm的最终值作为答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
int maxm = 0;
 
// Node structure
struct Node {
    int val;
 
    // Pointers to left
    // and right child
    Node *left, *right;
 
    // Intialize constructor
    Node(int x)
    {
        val = x;
        left = NULL;
        right = NULL;
    }
};
 
// Function to find the maximum
// count of setbits in a root to leaf
void maxm_setbits(Node* root, int ans)
{
    // Check if root is not null
    if (!root)
        return;
 
    if (root->left == NULL
        && root->right == NULL) {
 
        ans += __builtin_popcount(root->val);
 
        // Update the maximum count
        // of setbits
        maxm = max(ans, maxm);
 
        return;
    }
 
    // Traverse left of binary tree
    maxm_setbits(root->left,
                ans + __builtin_popcount(
                        root->val));
 
    // Traverse right of the binary tree
    maxm_setbits(root->right,
                ans + __builtin_popcount(
                        root->val));
}
 
// Driver Code
int main()
{
    Node* root = new Node(15);
    root->left = new Node(3);
    root->right = new Node(7);
    root->left->left = new Node(5);
    root->left->right = new Node(1);
    root->right->left = new Node(31);
    root->right->right = new Node(9);
 
    maxm_setbits(root, 0);
 
    cout << maxm << endl;
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
   
static int maxm = 0;
 
// Node structure
static class Node
{
    int val;
 
    // Pointers to left
    // and right child
    Node left, right;
 
    // Intialize constructor
    Node(int x)
    {
        val = x;
        left = null;
        right = null;
    }
};
 
// Function to find the maximum
// count of setbits in a root to leaf
static void maxm_setbits(Node root, int ans)
{
    // Check if root is not null
    if (root == null)
        return;
 
    if (root.left == null &&
        root.right == null)
    {
        ans += Integer.bitCount(root.val);
 
        // Update the maximum count
        // of setbits
        maxm = Math.max(ans, maxm);
 
        return;
    }
 
    // Traverse left of binary tree
    maxm_setbits(root.left,
                ans + Integer.bitCount(
                        root.val));
 
    // Traverse right of the binary tree
    maxm_setbits(root.right,
                ans + Integer.bitCount(
                        root.val));
}
 
// Driver Code
public static void main(String[] args)
{
    Node root = new Node(15);
    root.left = new Node(3);
    root.right = new Node(7);
    root.left.left = new Node(5);
    root.left.right = new Node(1);
    root.right.left = new Node(31);
    root.right.right = new Node(9);
 
    maxm_setbits(root, 0);
 
    System.out.print(maxm +"\n");
 
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
maxm = 0
 
# Node class
class Node:
     
    # Initialise constructor
    def __init__(self, x):
         
        self.val = x
        self.left = None
        self.right = None
         
# Function to count the number of 1 in number
def count_1(n):
     
    count = 0
    while (n):
        count += n & 1
        n >>= 1
         
    return count
 
# Function to find the maximum
# count of setbits in a root to leaf
def maxm_setbits(root, ans):
     
    global maxm
     
    # Check if root is null
    if not root:
        return
     
    if (root.left == None and
        root.right == None):
        ans += count_1(root.val)
         
        # Update the maximum count
        # of setbits
        maxm = max(ans, maxm)
        return
     
    # Traverse left of binary tree
    maxm_setbits(root.left,
                 ans + count_1(root.val))
     
    # Traverse right of the binary tree
    maxm_setbits(root.right,
                 ans + count_1(root.val))
     
# Driver code
root = Node(15)
root.left = Node(3)
root.right = Node(7)
root.left.left = Node(5)
root.left.right = Node(1)
root.right.left = Node(31)
root.right.right = Node(9)
 
maxm_setbits(root, 0)
 
print(maxm)
         
# This code is contributed by Stuti Pathak


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to Sort a Bitonic array
// in constant space
static void sortArr(int []a, int n)
{
    int i, k;
 
    // Initialse the value of k
    k = (int)(Math.Log(n) / Math.Log(2));
    k = (int) Math.Pow(2, k);
 
    // In each iteration compare elements
    // k distance apart and swap if
    // they are not in order
    while (k > 0)
    {
        for(i = 0; i + k < n; i++)
            if (a[i] > a[i + k])
            {
                int tmp = a[i];
                a[i] = a[i + k];
                a[i + k] = tmp;
            }
 
        // k is reduced to half
        // after every iteration
        k = k / 2;
    }
 
    // Print the array elements
    for(i = 0; i < n; i++)
    {
        Console.Write(a[i] + " ");
    }
}
     
// Driver code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 5, 20, 30, 40, 36,
                  33, 25, 15, 10 };
    int n = arr.Length;
     
    // Function call
    sortArr(arr, n);
}
}
 
// This code is contributed by gauravrajput1


输出:
12

时间复杂度: O(N),其中 N 表示节点数。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live