BM25 题解 | #二叉树的后序遍历#
二叉树的后序遍历
https://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541
解题思路:
递归,后序遍历,就是vist(T) 放在最后
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
List<Integer> res = new ArrayList<>();
postOrderNode(root, res);
int[] arr =new int[res.size()];
for(int i=0; i<res.size(); i++) {
arr[i] = res.get(i);
}
return arr;
}
private void postOrderNode(TreeNode root, List<Integer> res) {
if(root == null) return ;
postOrderNode(root.left, res);
postOrderNode(root.right, res);
res.add(root.val);
}
}