给定二叉树,任务是找到从根到叶的路径中所有节点的所有XOR值的最大值。
例子:
Input:
2
/ \
1 4
/ \
10 8
Output: 11
Explanation:
All the paths are:
2-1-10 XOR-VALUE = 9
2-1-8 XOR-VALUE = 11
2-4 XOR-VALUE = 6
Input:
2
/ \
1 4
/ \ / \
10 8 5 10
Output: 12
方法:
- 为了解决上述问题,我们必须使用前序遍历来递归遍历树。对于每个节点,请继续计算从根到当前节点的路径的XOR。
XOR of current node’s path = (XOR of the path till the parent) ^ (current node value)
- 如果该节点是左边的叶节点,而当前节点的右子节点为NULL,则我们计算max-Xor,如下所示:
max-Xor = max(max-Xor, cur-Xor).
下面是上述方法的实现:
C++
// C++ program to compute the
// Max-Xor value of path from
// the root to leaf of a Binary tree
#include
using namespace std;
// Binary tree node
struct Node {
int data;
struct Node *left, *right;
};
// Function to create a new node
struct Node* newNode(int data)
{
struct Node* newNode = new Node;
newNode->data = data;
newNode->left
= newNode->right = NULL;
return (newNode);
}
// Function calculate the
// value of max-xor
void Solve(Node* root, int xr,
int& max_xor)
{
// Updating the xor value
// with the xor of the
// path from root to
// the node
xr = xr ^ root->data;
// Check if node is leaf node
if (root->left == NULL
&& root->right == NULL) {
max_xor = max(max_xor, xr);
return;
}
// Check if the left
// node exist in the tree
if (root->left != NULL) {
Solve(root->left, xr,
max_xor);
}
// Check if the right node
// exist in the tree
if (root->right != NULL) {
Solve(root->right, xr,
max_xor);
}
return;
}
// Function to find the
// required count
int findMaxXor(Node* root)
{
int xr = 0, max_xor = 0;
// Recursively traverse
// the tree and compute
// the max_xor
Solve(root, xr, max_xor);
// Return the result
return max_xor;
}
// Driver code
int main(void)
{
// Create the binary tree
struct Node* root = newNode(2);
root->left = newNode(1);
root->right = newNode(4);
root->left->left = newNode(10);
root->left->right = newNode(8);
root->right->left = newNode(5);
root->right->right = newNode(10);
cout << findMaxXor(root);
return 0;
}
Python3
# Python3 program to compute the
# Max-Xor value of path from
# the root to leaf of a Binary tree
# Binary tree node
class Node:
# Function to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function calculate the
# value of max-xor
def Solve(root, xr, max_xor):
# Updating the xor value
# with the xor of the
# path from root to
# the node
xr = xr ^ root.data
# Check if node is leaf node
if (root.left == None and
root.right == None):
max_xor[0] = max(max_xor[0], xr)
# Check if the left
# node exist in the tree
if root.left != None:
Solve(root.left, xr, max_xor)
# Check if the right node
# exist in the tree
if root.right != None:
Solve(root.right, xr, max_xor)
return
# Function to find the
# required count
def findMaxXor(root):
xr, max_xor = 0, [0]
# Recursively traverse
# the tree and compute
# the max_xor
Solve(root, xr, max_xor)
# Return the result
return max_xor[0]
# Driver code
# Create the binary tree
root = Node(2)
root.left = Node(1)
root.right = Node(4)
root.left.left = Node(10)
root.left.right = Node(8)
root.right.left = Node(5)
root.right.right = Node(10)
print(findMaxXor(root))
# This code is contributed by Shivam Singh
输出:
12
时间复杂度:我们仅对每个节点进行一次迭代,因此将花费O(N)时间,其中N是二叉树中的节点数。
辅助空间复杂度:辅助空间复杂度将为O(1) ,因为没有使用额外的空间
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。