An open API service indexing awesome lists of open source software.

https://github.com/emahtab/binary-tree-postorder-traversal


https://github.com/emahtab/binary-tree-postorder-traversal

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# Binary Tree Postorder Traversal 🌲
## https://leetcode.com/problems/binary-tree-postorder-traversal

Given a binary tree, return the postorder traversal of its nodes' values.
```
Example:

Input: [1,null,2,3]
1
\
2
/
3

Output: [3,2,1]
```
**Follow up: Recursive solution is trivial, could you do it iteratively?**

# Postorder Traversal :
![Binary Tree](binary-tree.PNG?raw=true "Binary Tree")

Note that node 75 doesn't have left child and node 29 doesn't have right child.

The postorder traversal for above binary tree will be **[21, 20, 35, 30, 75, 67, 50, 29, 60, 65, 24, 43, 70]**

## Implementation : Recursive

```java
import java.util.ArrayList;
import java.util.List;

public class App {

public static void main(String[] args) {
TreeNode root = new TreeNode(70);
root.left = new TreeNode(67); root.right = new TreeNode(43);

root.left.left = new TreeNode(35); root.left.right = new TreeNode(75);

root.left.left.left = new TreeNode(21); root.left.left.right = new TreeNode(20);

root.left.right.right = new TreeNode(30);

root.right.left = new TreeNode(29); root.right.left.left = new TreeNode(50);

root.right.right = new TreeNode(24);

root.right.right.left = new TreeNode(60); root.right.right.right = new TreeNode(65);
System.out.println(inorderTraversal(root));
}


// Definition for a binary tree node.
static public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}


public static List inorderTraversal(TreeNode root) {
List res = new ArrayList();
helper(root, res);
return res;
}

public static void helper(TreeNode root, List res) {
if (root != null) {
helper(root.left, res);
helper(root.right, res);
res.add(root.val);
}
}
}

```
## Implementation : Iterative (One Stack)

```java
public List postorderTraversal(TreeNode root) {
List res = new ArrayList<>();
if(root == null)
return res;
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode current = stack.pop();
res.add(0,current.val);
if(current.left != null){
stack.push(current.left);
}
if(current.right != null){
stack.push(current.right);
}
}
return res;
}
```
#################################### OR ##############################
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List postorderTraversal(TreeNode root) {
List postorder = new ArrayList<>();
if(root == null)
return postorder;
Stack stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode current = stack.pop();
postorder.add(current.val);
if(current.left != null)
stack.push(current.left);
if(current.right != null)
stack.push(current.right);
}
Collections.reverse(postorder);
return postorder;
}
}
```

## Implementation : Iterative (Two Stack)

```java
public List postorderTraversal(TreeNode root) {
List res = new ArrayList<>();
if(root == null)
return res;
Stack stack = new Stack();
Stack values = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode current = stack.pop();
values.push(current.val);
if(current.left != null){
stack.push(current.left);
}
if(current.right != null){
stack.push(current.right);
}
}
while(!values.isEmpty()){
res.add(values.pop());
}
return res;
}
```

# References :
1. https://www.youtube.com/watch?v=sMI4RBEZyZ4
2. https://www.techiedelight.com/postorder-tree-traversal-iterative-recursive