- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 check whether we can color a tree where no adjacent nodes have the same color or not in python
Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color.
So, if the input is like
then the output will be True as we can get
To solve this, we will follow these steps −
- colors := an empty map
- prop := an empty map
- Define a function dfs() . This will take node, and flag
- if node is null, then
- return
- colors[value of node] := colors[value of node] + 1
- prop[flag] := prop[flag] + 1
- dfs(left of node, inverse of flag)
- dfs(right of node, inverse of flag)
- From the main method do the following:
- dfs(root, true)
- return true when all elements in colors and prop are same, otherwise false
Let us see the following implementation to get better understanding −
Example
from collections import defaultdict class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right class Solution: def solve(self, root): colors = defaultdict(int) prop = defaultdict(int) def dfs(node, flag=True): if not node: return colors[node.val] += 1 prop[flag] += 1 dfs(node.left, not flag) dfs(node.right, not flag) dfs(root) return set(colors.values()) == set(prop.values()) ob = Solution() root = TreeNode(2) root.left = TreeNode(2) root.right = TreeNode(2) root.right.left = TreeNode(1) root.right.right = TreeNode(1) root.right.left.right = TreeNode(1) print(ob.solve(root))
Input
root = TreeNode(2) root.left = TreeNode(2) root.right = TreeNode(2) root.right.left = TreeNode(1) root.right.right = TreeNode(1) root.right.left.right = TreeNode(1)
Output
True
- Related Articles
- Python program to check whether we can pile up cubes or not
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether given tree is symmetric tree or not in Python
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- Program to check whether we can get N queens solution or not in Python
- Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check all values in the tree are same or not in Python
- Program to form smallest number where no two adjacent digits are same in Python
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether all can get a seat or not in Python

Advertisements