-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreePostorderTraversal.java
53 lines (47 loc) · 1.59 KB
/
BinaryTreePostorderTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.namanh.binary_tree;
import com.namanh.common.TreeNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
/**
* Link: https://leetcode.com/problems/binary-tree-postorder-traversal
* Question: Given the root of a binary tree, return the postorder traversal of its nodes' values.
*/
public class BinaryTreePostorderTraversal {
/**
* Iterate approach
* S1: Similar preorder but reverse result
*/
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> nodeList = new ArrayList<>();
if (root == null) return nodeList;
Stack<TreeNode> nodeStack = new Stack<>();
nodeStack.push(root);
while (!nodeStack.isEmpty()) {
root = nodeStack.pop();
nodeList.add(root.val);
if (root.left != null) nodeStack.push(root.left);
if (root.right != null) nodeStack.push(root.right);
}
Collections.reverse(nodeList);
return nodeList;
}
/**
* Recursive approach
* S1: Check current node not null
* S2: Call recursive function for left child first, then right child
* S3: Add current node value to List
*/
public List<Integer> postorderTraversal2(TreeNode root) {
List<Integer> nodeList = new ArrayList<>();
getNode(nodeList, root);
return nodeList;
}
private void getNode(List<Integer> nodeList, TreeNode node) {
if (node == null) return;
getNode(nodeList, node.left);
getNode(nodeList, node.right);
nodeList.add(node.val);
}
}