标签:后序遍历 new example pre array assets tco lis set
给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 3叉树
:
返回其后序遍历: [5,6,3,2,4,1]
.
/* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) { val = _val; children = _children; } }; */ class Solution { List<Integer> list = new ArrayList<>(); public List<Integer> postorder(Node root) { if(root==null){return list;} scanPre(root); list.add(root.val); return list; } public void scanPre(Node node){ if(node.children.size()==0){return;} if(node.children.size()>0){ for(Node n:node.children){ scanPre(n); list.add(n.val); } } } }
标签:后序遍历 new example pre array assets tco lis set
原文地址:https://www.cnblogs.com/JAYPARK/p/10357764.html