-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode_07.java
33 lines (31 loc) · 905 Bytes
/
Leetcode_07.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
import java.util.ArrayList;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Leetcode_07 {
private ArrayList<Integer> nodes=new ArrayList<Integer>();
public ArrayList<Integer> postorderTraversal(TreeNode root) {
if(root==null)
return nodes;
if(root.left!=null)
postorderTraversal(root.left);
if(root.right!=null)
postorderTraversal(root.right);
nodes.add(root.val);
return nodes;
}
public static void main(String[] args)
{
TreeNode root=new TreeNode(1);
TreeNode right=new TreeNode(2);
root.right=right;
TreeNode left=new TreeNode(3);
right.left=left;
ArrayList<Integer> list=new Leetcode_07().postorderTraversal(root);
for(int value : list)
System.out.print(value+" ");
}
}