Skip to content

Latest commit

 

History

History
 
 

nAryTreePostorderTraversal

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

N叉树的后序遍历

LeetCode地址

递归方法

public class Solution {

    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        helper(root, res);
        return res;
    }

    private void helper(Node root, List<Integer> res) {
        if (root != null) {
            if (root.children != null)
                for (int i = 0; i < root.children.size(); i++)
                    helper(root.children.get(i), res);
            res.add(root.val);
        }
    }
}

迭代没看懂。。。