题解 | #二叉树的后序遍历#
二叉树的后序遍历
https://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541
import java.util.*; public class Solution { public int[] postorderTraversal (TreeNode root) { if (root == null){ return new int[0]; } List<Integer> ll = new ArrayList<>(); TreeNode tn = root; if (root.left != null) { tn = root.left; int[] left = postorderTraversal(tn); for (int j = 0; j < left.length; j++){ ll.add(left[j]); } } if (root.right != null) { tn = root.right; int[] right = postorderTraversal(tn); for (int j = 0; j < right.length; j++) ll.add(right[j]); } ll.add(root.val); int[] result = new int[ll.size()]; for (int i = 0; i < ll.size(); i++) { result[i] = ll.get(i); } return result; } }