- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to delete all leaves with even values from a binary tree in Python
Suppose we have we have a binary tree, we will repeatedly delete all leaves that have even values. After deleting all, if it has only root with even values, that will be deleted also.
So, if the input is like
then the output will be
To solve this, we will follow these steps −
Define a function solve() . This will take root
if root is null, then
return null
left of root := solve(left of root)
right of root := solve(right of root)
if root is leaf and data of root is even, then
return null
return root
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right def inorder(root): if root: inorder(root.left) print(root.data, end = ', ') inorder(root.right) class Solution: def solve(self, root): if not root: return None root.left = self.solve(root.left) root.right = self.solve(root.right) if not root.left and not root.right and root.data % 2 == 0: return None return root ob = Solution() root = TreeNode(13) root.left = TreeNode(12) root.right = TreeNode(14) root.right.left = TreeNode(16) root.right.right = TreeNode(22) root.right.left.left = TreeNode(4) root.right.left.right = TreeNode(7) ob.solve(root) inorder(root)
Input
root = TreeNode(13) root.left = TreeNode(12) root.right = TreeNode(14) root.right.left = TreeNode(16) root.right.right = TreeNode(22) root.right.left.left = TreeNode(4) root.right.left.right = TreeNode(7)
Output
13, 16, 7, 14,
- Related Articles
- Program to remove all nodes with only one child from a binary tree in Python?
- Program to find longest even value path of a binary tree in Python
- Print all nodes in a binary tree having K leaves in C++
- Find sum of all left leaves in a given Binary Tree in C++
- Find sum of all right leaves in a given Binary Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to invert a binary tree in Python
- Deleting a binary tree using the delete keyword in C++ program
- Program to find sum of the right leaves of a binary tree in C++
- Find Leaves of Binary Tree in C++
- Program to find largest binary search subtree from a given tree in Python
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Delete all the even nodes from a Doubly Linked List in C++
- Program to find sum of all numbers formed by path of a binary tree in python

Advertisements