📌  相关文章
📜  计算具有给定XOR值的二叉树从根到叶的路径数

📅  最后修改于: 2021-04-25 00:39:22             🧑  作者: Mango

给定一个值K和一个二叉树,我们必须找出从根到叶节点的路径总数,该路径的所有节点沿路径的XOR等于K。
例子:

Input: K = 6
       2
      / \
     1   4
    / \   
   10  5   
Output: 2
Explanation:
Subtree 1: 
   2
    \
     4
This particular path has 2 nodes, 2 and 4 
and (2 xor 4) = 6.

Subtree 2:
        2
       /
      1
       \
        5
This particular path has 3 nodes; 2, 1 and 5 
and (2 xor 1 xor 5) = 6.


方法:
为了解决上述问题,我们必须使用顺序遍历来递归遍历树。对于每个节点,请继续计算从根到当前节点的路径的XOR。

如果该节点是左边的叶节点,而当前节点的右子节点为NULL,则我们检查路径的xor值是否为K,如果为x,则增加计数,否则不执行任何操作。最后,打印计数中的值。
下面是上述方法的实现:

C++
// C++ program to Count the number of
// path from the root to leaf of a
// Binary tree with given XOR value
 
#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);
}
 
void Count(Node* root, int xr, int& res, int& k)
{
 
    // 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) {
 
        if (xr == k) {
            res++;
        }
        return;
    }
 
    // check if the left
    // node exist in the tree
    if (root->left != NULL) {
        Count(root->left, xr, res, k);
    }
 
    // check if the right node
    // exist in the tree
    if (root->right != NULL) {
        Count(root->right, xr, res, k);
    }
 
    return;
}
 
// Function to find the required count
int findCount(Node* root, int K)
{
 
    int res = 0, xr = 0;
 
    // recursively traverse the tree
    // and compute the count
    Count(root, xr, res, K);
 
    // return the result
    return res;
}
 
// 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(5);
 
    int K = 6;
 
    cout << findCount(root, K);
 
    return 0;
}


Java
// Java program to Count the number of
// path from the root to leaf of a
// Binary tree with given XOR value
import java.util.*;
 
class GFG{
  
// Binary tree node
static class Node {
    int data;
  
    Node left, right;
};
static int res, k;
 
// Function to create a new node
static Node newNode(int data)
{
    Node newNode = new Node();
  
    newNode.data = data;
  
    newNode.left
        = newNode.right = null;
  
    return (newNode);
}
  
static void Count(Node root, int xr)
{
  
    // 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) {
  
        if (xr == k) {
            res++;
        }
        return;
    }
  
    // check if the left
    // node exist in the tree
    if (root.left != null) {
        Count(root.left, xr);
    }
  
    // check if the right node
    // exist in the tree
    if (root.right != null) {
        Count(root.right, xr);
    }
  
    return;
}
  
// Function to find the required count
static int findCount(Node root, int K)
{
  
    int xr = 0;
    res = 0;
    k = K;
 
    // recursively traverse the tree
    // and compute the count
    Count(root, xr);
  
    // return the result
    return res;
}
  
// Driver code
public static void main(String[] args)
{
    // Create the binary tree
    Node root = newNode(2);
    root.left = newNode(1);
    root.right = newNode(4);
    root.left.left = newNode(10);
    root.left.right = newNode(5);
  
    int K = 6;
  
    System.out.print(findCount(root, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to Count
# the number of path from
# the root to leaf of a
# Binary tree with given XOR value
 
# Binary tree node
class Node:
    def __init__(self, data):
       
        self.data = data
        self.left = None
        self.right = None
 
def Count(root : Node,
          xr : int) -> None:
   
    global K, res
 
    # 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 is None and
        root.right is None):
        if (xr == K):
            res += 1
 
        return
 
    # Check if the left
    # node exist in the tree
    if (root.left):
        Count(root.left, xr)
 
    # Check if the right node
    # exist in the tree
    if (root.right):
        Count(root.right, xr)
 
    return
 
# Function to find the
# required count
def findCount(root : Node) -> int:
   
    global K, res
    xr = 0
 
    # Recursively traverse the tree
    # and compute the count
    Count(root, xr)
 
    # return the result
    return res
 
# Driver code
if __name__ == "__main__":
 
    # Create the binary tree
    root = Node(2)
    root.left = Node(1)
    root.right = Node(4)
    root.left.left = Node(10)
    root.left.right = Node(5)
 
    K = 6
    res = 0
    print(findCount(root))
 
# This code is contributed by sanjeev2552


C#
// C# program to Count the number of
// path from the root to leaf of a
// Binary tree with given XOR value
using System;
 
class GFG{
   
// Binary tree node
class Node {
    public int data;
   
    public Node left, right;
};
static int res, k;
  
// Function to create a new node
static Node newNode(int data)
{
    Node newNode = new Node();
   
    newNode.data = data;
   
    newNode.left
        = newNode.right = null;
   
    return (newNode);
}
   
static void Count(Node root, int xr)
{
   
    // 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) {
   
        if (xr == k) {
            res++;
        }
        return;
    }
   
    // check if the left
    // node exist in the tree
    if (root.left != null) {
        Count(root.left, xr);
    }
   
    // check if the right node
    // exist in the tree
    if (root.right != null) {
        Count(root.right, xr);
    }
   
    return;
}
   
// Function to find the required count
static int findCount(Node root, int K)
{
   
    int xr = 0;
    res = 0;
    k = K;
  
    // recursively traverse the tree
    // and compute the count
    Count(root, xr);
   
    // return the result
    return res;
}
   
// Driver code
public static void Main(String[] args)
{
    // Create the binary tree
    Node root = newNode(2);
    root.left = newNode(1);
    root.right = newNode(4);
    root.left.left = newNode(10);
    root.left.right = newNode(5);
   
    int K = 6;
   
    Console.Write(findCount(root, K));
}
}
 
// This code is contributed by Princi Singh


输出:
2


时间复杂度:与上述方法一样,我们在每个节点上仅迭代一次,因此将花费O(N)时间,其中N是二叉树中的节点数。
辅助空间:与上述方法一样,没有使用额外的空间,因此辅助空间的复杂度将为O(1)。