📌  相关文章
📜  从二叉树中删除节点,以使所有剩余的根到叶路径的总和至少为K

📅  最后修改于: 2021-04-30 02:14:15             🧑  作者: Mango

给定二叉树和整数K ,任务是从给定树中删除节点,以使所有剩余的根到叶路径的所有节点的总和至少为K。

例子:

方法:想法是使用递归并执行Postorder Traversal并删除那些添加到路径总和小于K的节点。步骤如下:

  • 在给定的树上执行后序遍历,并在遍历期间将所有节点的总和从根节点传递到每个节点。
  • 在遍历期间,如果到达叶节点,则检查直到该节点的所有节点的总和是否小于K? 。如果发现是真的,则通过从该节点返回NULL节点来删除该节点。
  • 对更新树中遇到的每个叶节点重复上述步骤。
  • 完成上述步骤后,打印修改后的Tree的Preorder Traversal。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Tree Node Class
struct Node
{
    int data;
    Node *left;
    Node *right;
 
    Node(int x)
    {
        data = x;
        left = right = NULL;
    }
};
 
// Utility function that deletes nodes
// from the Tree such that every root
// to leaf path sum is at least K
Node *removePathLessThanK(Node *node, int K,
                          int sum)
{
     
    // Base Case
    if (node == NULL)
    {
        return NULL;
    }
 
    // Recurse to the left
    if (node->left != NULL)
    {
        node->left = removePathLessThanK(
                     node->left, K,
                     sum + node->left->data);
    }
 
    // Recurse to the right
    if (node->right != NULL)
    {
        node->right = removePathLessThanK(
                      node->right, K,
                      sum + node->right->data);
    }
 
    // Check path sum at leaf node
    // is lesser than K, return NULL
    if (node->left == NULL &&
        node->right == NULL && sum < K)
    {
        node = NULL;
        return node;
    }
 
    // Otherwise return the
    // current node as it is
    return node;
}
 
// Function to print the preorder
// traversal of the Tree
void viewTree(Node *node)
{
     
    // If node is not NULL
    if (node != NULL)
    {
         
        // Print the node
        cout << node->data << " ";
 
        // Left and Right Traversal
        viewTree(node->left);
        viewTree(node->right);
    }
}
 
// Function that deletes the nodes
// from the Tree such that every root
// to leaf path sum is at least K
void removePathLessThanKUtil(Node *node, int K,
                             int sum)
{
     
    // Function Call to delete Nodes
    Node *result = removePathLessThanK(node, K, sum);
     
    // Preorder Traversal of the
    // modified Tree
    viewTree(result);
}
 
// Driver Code
int main()
{
     
    // Given sum K
    int K = 27;
 
    // Given Binary Tree
    Node *root = NULL;
    root = new Node(5);
    root->right = new Node(3);
    root->left = new Node(4);
    root->left->left = new Node(9);
    root->right->right = new Node(9);
    root->left->right = new Node(8);
    root->left->right->right = new Node(11);
    root->left->right->left = new Node(5);
    root->left->right->left->left = new Node(6);
    root->left->right->left->right = new Node(2);
    root->right->right->right = new Node(4);
 
    // Function Call
    removePathLessThanKUtil(root, K, root->data);
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
// Tree Node Class
class Node {
    int data;
    Node left;
    Node right;
}
 
class path {
 
    // Function to insert node in the
    // given Binary Tree
    public Node insert(int val)
    {
        Node n = new Node();
        n.data = val;
        n.left = null;
        n.right = null;
        return n;
    }
 
    // Utility function that deletes nodes
    // from the Tree such that every root
    // to leaf path sum is at least K
    public Node removePathLessThanK(
        Node node, int K, int sum)
    {
        // Base Case
        if (node == null) {
            return null;
        }
 
        // Recurse to the left
        if (node.left != null) {
            node.left
                = removePathLessThanK(
                    node.left, K,
                    sum + node.left.data);
        }
 
        // Recurse to the right
        if (node.right != null) {
            node.right
                = removePathLessThanK(
                    node.right, K,
                    sum + node.right.data);
        }
 
        // Check path sum at leaf node
        // is lesser than K, return NULL
        if (node.left == null
            && node.right == null
            && sum < K) {
            node = null;
            return node;
        }
 
        // Otherwise return the
        // current node as it is
        return node;
    }
 
    // Function to print the preorder
    // traversal of the Tree
    public void viewTree(Node node)
    {
        // If node is not NULL
        if (node != null) {
 
            // Print the node
            System.out.print(node.data + " ");
 
            // Left and Right Traversal
            viewTree(node.left);
            viewTree(node.right);
        }
    }
 
    // Function that deletes the nodes
    // from the Tree such that every root
    // to leaf path sum is at least K
    public void removePathLessThanKUtil(
        Node node, int K, int sum)
    {
        // Function Call to delete Nodes
        Node result = removePathLessThanK(
            node, K, sum);
 
        // Preorder Traversal of the
        // modified Tree
        viewTree(result);
    }
}
 
// Driver Code
class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given sum K
        int K = 27;
 
        // Object of class path
        path b = new path();
 
        // Given Binary Tree
        Node root = null;
        root = b.insert(5);
        root.right = b.insert(3);
        root.left = b.insert(4);
        root.left.left = b.insert(9);
        root.right.right = b.insert(9);
        root.left.right = b.insert(8);
        root.left.right.right = b.insert(11);
        root.left.right.left = b.insert(5);
        root.left.right.left.left = b.insert(6);
        root.left.right.left.right = b.insert(2);
        root.right.right.right = b.insert(4);
 
        // Function Call
        b.removePathLessThanKUtil(
            root, K, root.data);
    }
}


Python3
# Python3 program for the above approach
 
# Tree Node Class
class newNode:
     
    def __init__(self, x):
         
        self.data = x
        self.left = None
        self.right = None
 
# Utility function that deletes nodes
# from the Tree such that every root
# to leaf path sum is at least K
def removePathLessThanK(node, K, sum):
     
    # Base Case
    if (node == None):
        return None
 
    # Recurse to the left
    if (node.left != None):
        node.left = removePathLessThanK(
                    node.left, K,
                    sum + node.left.data)
 
    # Recurse to the right
    if (node.right != None):
        node.right = removePathLessThanK(
                     node.right, K,
                     sum + node.right.data)
 
    # Check path sum at leaf node
    # is lesser than K, return None
    if (node.left == None and
       node.right == None and sum < K):
        node = None
        return node
 
    # Otherwise return the
    # current node as it is
    return node
 
# Function to print the preorder
# traversal of the Tree
def viewTree(node):
     
    # If node is not None
    if (node != None):
         
        # Print the node
        print(node.data, end = " ")
 
        # Left and Right Traversal
        viewTree(node.left)
        viewTree(node.right)
 
# Function that deletes the nodes
# from the Tree such that every root
# to leaf path sum is at least K
def removePathLessThanKUtil(node, K, sum):
     
    # Function Call to delete Nodes
    result = removePathLessThanK(node, K, sum)
     
    # Preorder Traversal of the
    # modified Tree
    viewTree(result)
 
# Driver Code
if __name__ == '__main__':
     
    # Given sum K
    K = 27
 
    # Given Binary Tree
    root = None
    root = newNode(5)
    root.right = newNode(3)
    root.left = newNode(4)
    root.left.left = newNode(9)
    root.right.right = newNode(9)
    root.left.right = newNode(8)
    root.left.right.right = newNode(11)
    root.left.right.left = newNode(5)
    root.left.right.left.left = newNode(6)
    root.left.right.left.right = newNode(2)
    root.right.right.right = newNode(4)
 
    # Function Call
    removePathLessThanKUtil(root, K, root.data)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
// Tree Node Class
public class Node
{
    public int data;
    public Node left;
    public Node right;
}
 
class path{
 
// Function to insert node in the
// given Binary Tree
public Node insert(int val)
{
    Node n = new Node();
    n.data = val;
    n.left = null;
    n.right = null;
    return n;
}
 
// Utility function that deletes nodes
// from the Tree such that every root
// to leaf path sum is at least K
public Node removePathLessThanK(Node node,
                                int K, int sum)
{
     
    // Base Case
    if (node == null)
    {
        return null;
    }
 
    // Recurse to the left
    if (node.left != null)
    {
        node.left = removePathLessThanK(
                    node.left, K,
                    sum + node.left.data);
    }
 
    // Recurse to the right
    if (node.right != null)
    {
        node.right = removePathLessThanK(
                     node.right, K,
                     sum + node.right.data);
    }
 
    // Check path sum at leaf node
    // is lesser than K, return NULL
    if (node.left == null &&
       node.right == null && sum < K)
    {
        node = null;
        return node;
    }
 
    // Otherwise return the
    // current node as it is
    return node;
}
 
// Function to print the preorder
// traversal of the Tree
public void viewTree(Node node)
{
     
    // If node is not NULL
    if (node != null)
    {
         
        // Print the node
        Console.Write(node.data + " ");
 
        // Left and Right Traversal
        viewTree(node.left);
        viewTree(node.right);
    }
}
 
// Function that deletes the nodes
// from the Tree such that every root
// to leaf path sum is at least K
public void removePathLessThanKUtil(Node node,
                                    int K, int sum)
{
     
    // Function Call to delete Nodes
    Node result = removePathLessThanK(
        node, K, sum);
 
    // Preorder Traversal of the
    // modified Tree
    viewTree(result);
}
}
 
// Driver Code
class GFG{
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given sum K
    int K = 27;
 
    // Object of class path
    path b = new path();
 
    // Given Binary Tree
    Node root = null;
    root = b.insert(5);
    root.right = b.insert(3);
    root.left = b.insert(4);
    root.left.left = b.insert(9);
    root.right.right = b.insert(9);
    root.left.right = b.insert(8);
    root.left.right.right = b.insert(11);
    root.left.right.left = b.insert(5);
    root.left.right.left.left = b.insert(6);
    root.left.right.left.right = b.insert(2);
    root.right.right.right = b.insert(4);
 
    // Function Call
    b.removePathLessThanKUtil(
        root, K, root.data);
}
}
 
// This code is contributed by Princi Singh


输出:
5 4 8 5 6 11

时间复杂度: O(N),其中N是给定Tree中的节点数。
辅助空间: O(N)