后序遍历二叉树

package leetcode;
/*Given a binary tree, return the postorder traversal of its nodes' values.
*/
import java.util.ArrayList;

public class Binary_tree_postorder_traversal {
     public static void main(String[] args) {
        
    }
     
     public ArrayList<Integer> postorderTraversal(TreeNode root) {
         ArrayList<Integer> al = new ArrayList<>();
         if(root == null)
             return al;
         al.addAll(postorderTraversal(root.left));  //注意addAll的用法,即便每次递归新建立al但是addall按照指定collection迭代器所返回的元素顺序,追加到列表末尾
         al.addAll(postorderTraversal(root.right));
         al.add(root.val);
        return al;
         
     }

    
}
 

猜你喜欢

转载自blog.csdn.net/ZWB626/article/details/84780602
今日推荐