-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathr2.cpp
More file actions
74 lines (59 loc) · 1.26 KB
/
r2.cpp
File metadata and controls
74 lines (59 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Initialise to update the maximum
// AND value from all the path
int maxm = 0;
// Node structure
struct Node {
int val;
// Left & right child of the node
Node *left, *right;
// Intialize consutructor
Node(int x)
{
val = x;
left = NULL;
right = NULL;
}
};
// Function to find the maximum value
// of Bitwise AND from root to leaf
// in a Binary tree
void maxm_Anding(Node* root, int ans)
{
// Check if root is not null
if (!root)
return;
if (root->left == NULL
and root->right == NULL) {
ans &= root->val;
// Find the maxmimum AND value and
// store in global maxm varibale
maxm = max(ans, maxm);
return;
}
// Traverse left of binary tree
maxm_Anding(root->left,
ans & root->val);
// Traverse right of the binary tree
maxm_Anding(root->right,
ans & root->val);
}
// Driver Code
int main()
{
// Given Tree
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);
// Function Call
maxm_Anding(root, root->val);
// Print the maximum AND value
cout << maxm << endl;
return 0;
}