leetcode590. N叉树的后序遍历
生活随笔
收集整理的這篇文章主要介紹了
leetcode590. N叉树的后序遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個 N 叉樹,返回其節點值的后序遍歷。
例如,給定一個?3叉樹?:
思路:先遍歷所有孩子,再放入自己。
/* // Definition for a Node. class Node {public int val;public List<Node> children;public Node() {}public Node(int _val) {val = _val;}public Node(int _val, List<Node> _children) {val = _val;children = _children;} }; */ class Solution {List<Integer> res=new ArrayList<Integer>();public List<Integer> postorder(Node root) {helper(root);return res;}public void helper(Node root){if (root==null) return;for (int i = 0; i <root.children.size() ; i++) {helper(root.children.get(i));}res.add(root.val);} }?
總結
以上是生活随笔為你收集整理的leetcode590. N叉树的后序遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 损失险包括哪些 损失险包括了哪些
- 下一篇: 学习笔记4-C语言-开关、循环、跳转