LeetCode - 145. Binary Tree Postorder Traversal(Tree, Stack, DFS, Binary Tree)

YAMAMAMO·2023년 1월 6일
0

LeetCode

목록 보기
99/100
post-thumbnail

문제

Given the root of a binary tree, return the postorder traversal of its nodes' values.

https://leetcode.com/problems/binary-tree-postorder-traversal/description/

Example 1:

Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

풀이

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        postOrder(root, res);
        return res;
    }

    public void postOrder(TreeNode root, List<Integer> res){
        if(root == null) return;
        postOrder(root.left, res);
        postOrder(root.right, res);
        res.add(root.val);
    }
}
profile
안드로이드 개발자

0개의 댓글