Given a binary tree, a complete path is defined as a path from root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given a number K, you have to remove (prune the tree) all nodes which don’t lie in any path with sum>=k.
Note: A node can be part of multiple paths. So we have to delete it only in case when all paths from it have sum less than K.
给定一个二叉树,一条完整路径被定义为从根结点到叶子节点的一条路径。这条路径上的节点的和定义为这条路径的和。给定一个数K,去掉那些不在remove (prune the tree) all nodes which don’t lie in any path with sum>=k.
Consider the following Binary Tree 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / / 8 9 12 10 / \ \ 13 14 11 / 15 For input k = 20, the tree should be changed to following (Nodes with values 6 and 8 are deleted) 1 / \ 2 3 / \ \ 4 5 7 \ / / 9 12 10 / \ \ 13 14 11 / 15 For input k = 45, the tree should be changed to following. 1 / 2 / 4 \ 9 \ 14 / 15
代码1和代码2的区别在于,代码1的pruneUtil函数要返回根结点,代码2的pruneUtil函数不返回根结点。但是代码1有runtime error,代码2正确无误。以后还要研究一下。
代码1如下:
/* Program to find LCA of n1 and n2 using one traversal of Binary Tree */ #include <iostream> #include <vector> #include <string> using namespace std; #include <queue> // std::priority_queue #include <vector> // std::vector #include <functional> // std::greater #include<stdio.h> #include <stdio.h> #include <stdlib.h> // A Binary Tree Node struct Node { int data; Node *left, *right; }; // A utility function to create a new Binary Tree node with given data Node* newNode(int data) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = data; node->left = node->right = NULL; return node; } // print the tree in LVR (Inorder traversal) way. void print(Node*root) { if (root != NULL) { print(root->left); printf("%d ",root->data); print(root->right); } } /* Main function which truncates the binary tree. */ void pruneUtil(Node*root, int k, int *sum) { // Base Case if (root == NULL) return; // Initialize left and right sums as sum from root to // this node (including this node) int lsum = *sum + (root->data); int rsum = lsum; // Recursively prune left and right subtrees pruneUtil(root->left, k, &lsum); pruneUtil(root->right, k, &rsum); // Get the maximum of left and right sums *sum = max(lsum, rsum); // If maximum is smaller than k, then this node // must be deleted if (*sum < k) { free(root); root = NULL; } } // A wrapper over pruneUtil() void prune(Node*root, int k) { int sum = 0; pruneUtil(root, k, &sum); } // Driver program to test above function int main() { int k = 45; Node*root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->left->left->left = newNode(8); root->left->left->right = newNode(9); root->left->right->left = newNode(12); root->right->right->left = newNode(10); root->right->right->left->right = newNode(11); root->left->left->right->left = newNode(13); root->left->left->right->right = newNode(14); root->left->left->right->right->left = newNode(15); printf("Tree before truncation\n"); print(root); prune(root, k); // k is 45 printf("\n\nTree after truncation\n"); print(root); return 0; }
#include <stdio.h> #include <stdlib.h> // A utility function to get maximum of two integers int max(int l, int r) { return (l > r ? l : r); } // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // A utility function to create a new Binary Tree node with given data struct Node* newNode(int data) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = data; node->left = node->right = NULL; return node; } // print the tree in LVR (Inorder traversal) way. void print(struct Node *root) { if (root != NULL) { print(root->left); printf("%d ",root->data); print(root->right); } } /* Main function which truncates the binary tree. */ struct Node *pruneUtil(struct Node *root, int k, int *sum) { // Base Case if (root == NULL) return NULL; // Initialize left and right sums as sum from root to // this node (including this node) int lsum = *sum + (root->data); int rsum = lsum; // Recursively prune left and right subtrees root->left = pruneUtil(root->left, k, &lsum); root->right = pruneUtil(root->right, k, &rsum); // Get the maximum of left and right sums *sum = max(lsum, rsum); // If maximum is smaller than k, then this node // must be deleted if (*sum < k) { free(root); root = NULL; } return root; } // A wrapper over pruneUtil() struct Node *prune(struct Node *root, int k) { int sum = 0; return pruneUtil(root, k, &sum); } // Driver program to test above function int main() { int k = 45; struct Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->left->left->left = newNode(8); root->left->left->right = newNode(9); root->left->right->left = newNode(12); root->right->right->left = newNode(10); root->right->right->left->right = newNode(11); root->left->left->right->left = newNode(13); root->left->left->right->right = newNode(14); root->left->left->right->right->left = newNode(15); printf("Tree before truncation\n"); print(root); root = prune(root, k); // k is 45 printf("\n\nTree after truncation\n"); print(root); return 0; }