DEV Community

Cover image for Day 8 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 8 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/postorder-traversal/1

Postorder Traversal

*Difficulty: Easy Accuracy: 74.96% *

Given the root of a Binary Tree, your task is to return its Postorder Traversal.
Note: A postorder traversal first visits the left child (including its entire subtree), then visits the right child (including its entire subtree), and finally visits the node itself.
Examples:
Input: root = [19, 10, 8, 11, 13]

Output: [11, 13, 10, 8, 19]
Explanation: The postorder traversal of the given binary tree is [11, 13, 10, 8, 19].

Input: root = [11, 15, N, 7]


Output: [7, 15, 11]
Explanation: The postorder traversal of the given binary tree is [7, 15, 11]

Constraints:
1 ≀ number of nodes ≀ 3*104
0 ≀ node->data ≀ 105

Solution:
class Solution:
def postOrder(self, root):
res = []
def dfs(node):
if not node:
return
dfs(node.left)
dfs(node.right)
res.append(node.data)
dfs(root)
return res

Top comments (0)