给定一个二叉树,任务是删除不包含任何奇数值节点的所有子树。删除这些子树后,打印树的“层级遍历”。
注意:对于已删除的节点,打印NULL 。
例子:
Input: Below is the given Tree:
1
\
2
/ \
8 5
Output: 1 NULL 2 NULL 5
Explanation:
Tree after pruning:
1
\
2
\
5
Input: Below is the given Tree:
1
/ \
2 7
/ \ / \
8 10 12 5
Output: 1 NULL 7 NULL 5
Explanation:
Tree after pruning:
1
\
7
\
5
方法:要解决给定的问题,其想法是使用DFS遍历遍历树,并将使用回溯的概念。请按照以下步骤解决问题:
- 使用DFS遍历遍历给定的树并执行以下步骤:
- 如果根节点为NULL ,则返回。
- 如果叶节点值是偶数,则通过返回NULL来删除此节点。否则,请从当前递归调用中返回根节点。
- 使用以上条件,递归更新左子树和右子树。
- 完成上述步骤后,使用级别为NULL的遍历顺序打印已更新的树,以代替已删除的节点。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Node of the tree
struct node {
int data;
struct node *left, *right;
};
// Function to create a new node
node* newNode(int key)
{
node* temp = new node;
temp->data = key;
temp->left = temp->right = NULL;
return (temp);
}
// Function to print tree level wise
void printLevelOrder(node* root)
{
// Base Case
if (!root)
return;
// Create an empty queue for
// level order traversal
queue q;
// Enqueue Root
q.push(root);
while (!q.empty()) {
// Print front of queue and
// remove it from queue
node* temp = q.front();
cout << temp->data << " ";
q.pop();
// If left child is present
if (temp->left != NULL) {
q.push(temp->left);
}
// Otherwise
else if (temp->right != NULL) {
cout << "NULL ";
}
// If right child is present
if (temp->right != NULL) {
q.push(temp->right);
}
// Otherwise
else if (temp->left != NULL) {
cout << "NULL ";
}
}
}
// Function to remove subtrees
node* pruneTree(node* root)
{
// Base Case
if (!root) {
return NULL;
}
// Search for required condition
// in left and right half
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
// If the node is even
// and leaf node
if (root->data % 2 == 0
&& !root->right
&& !root->left)
return NULL;
return root;
}
// Driver Code
int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(8);
root->left->right = newNode(10);
root->right = newNode(7);
root->right->left = newNode(12);
root->right->right = newNode(5);
// Function Call
node* newRoot = pruneTree(root);
// Print answer
printLevelOrder(newRoot);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Node of the tree
static class node
{
int data;
node left, right;
};
// Function to create a new node
static node newNode(int key)
{
node temp = new node();
temp.data = key;
temp.left = temp.right = null;
return (temp);
}
// Function to print tree level wise
static void printLevelOrder(node root)
{
// Base Case
if (root==null)
return;
// Create an empty queue for
// level order traversal
Queue q = new LinkedList<>();
// Enqueue Root
q.add(root);
while (!q.isEmpty())
{
// Print front of queue and
// remove it from queue
node temp = q.peek();
System.out.print(temp.data+ " ");
q.remove();
// If left child is present
if (temp.left != null)
{
q.add(temp.left);
}
// Otherwise
else if (temp.right != null)
{
System.out.print("null ");
}
// If right child is present
if (temp.right != null)
{
q.add(temp.right);
}
// Otherwise
else if (temp.left != null)
{
System.out.print("null ");
}
}
}
// Function to remove subtrees
static node pruneTree(node root)
{
// Base Case
if (root == null)
{
return null;
}
// Search for required condition
// in left and right half
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
// If the node is even
// and leaf node
if (root.data % 2 == 0
&& root.right==null
&& root.left==null)
return null;
return root;
}
// Driver Code
public static void main(String[] args)
{
node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(8);
root.left.right = newNode(10);
root.right = newNode(7);
root.right.left = newNode(12);
root.right.right = newNode(5);
// Function Call
node newRoot = pruneTree(root);
// Print answer
printLevelOrder(newRoot);
}
}
// This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Node of the tree
class node
{
public int data;
public node left, right;
};
// Function to create a new node
static node newNode(int key)
{
node temp = new node();
temp.data = key;
temp.left = temp.right = null;
return (temp);
}
// Function to print tree level wise
static void printLevelOrder(node root)
{
// Base Case
if (root == null)
return;
// Create an empty queue for
// level order traversal
Queue q = new Queue();
// Enqueue Root
q.Enqueue(root);
while (q.Count != 0)
{
// Print front of queue and
// remove it from queue
node temp = q.Peek();
Console.Write(temp.data+ " ");
q.Dequeue();
// If left child is present
if (temp.left != null)
{
q.Enqueue(temp.left);
}
// Otherwise
else if (temp.right != null)
{
Console.Write("null ");
}
// If right child is present
if (temp.right != null)
{
q.Enqueue(temp.right);
}
// Otherwise
else if (temp.left != null)
{
Console.Write("null ");
}
}
}
// Function to remove subtrees
static node pruneTree(node root)
{
// Base Case
if (root == null)
{
return null;
}
// Search for required condition
// in left and right half
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
// If the node is even
// and leaf node
if (root.data % 2 == 0
&& root.right==null
&& root.left==null)
return null;
return root;
}
// Driver Code
public static void Main(String[] args)
{
node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(8);
root.left.right = newNode(10);
root.right = newNode(7);
root.right.left = newNode(12);
root.right.right = newNode(5);
// Function Call
node newRoot = pruneTree(root);
// Print answer
printLevelOrder(newRoot);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
# Node of the tree
class node:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
# Function to print tree level wise
def printLevelOrder(root):
# Base Case
if (root==None):
return
# Create an empty queue for
# level order traversal
q = []
# Enqueue Root
q.append(root)
while(len(q)>0):
# Print front of queue and
# remove it from queue
temp = q[0]
print(temp.data,end = " ")
q = q[1:]
# If left child is present
if (temp.left != None):
q.append(temp.left)
# Otherwise
elif (temp.right != None):
print("NULL",end= " ")
# If right child is present
if (temp.right != None):
q.append(temp.right)
# Otherwise
elif (temp.left != None):
print("NULL",end = " ")
# Function to remove subtrees
def pruneTree(root):
# Base Case
if (root==None):
return None
# Search for required condition
# in left and right half
root.left = pruneTree(root.left)
root.right = pruneTree(root.right)
# If the node is even
# and leaf node
if (root.data % 2 == 0 and root.right == None and root.left==None):
return None
return root
# Driver Code
if __name__ == '__main__':
root = node(1)
root.left = node(2)
root.left.left = node(8)
root.left.right = node(10)
root.right = node(7)
root.right.left = node(12)
root.right.right = node(5)
# Function Call
newRoot = pruneTree(root)
# Print answer
printLevelOrder(newRoot)
# This code is contributed by SURENDRA_GANGWAR.
1 NULL 7 NULL 5
时间复杂度: O(N)
辅助空间: O(1)