• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

LeetCode 590. N-ary Tree Postorder Traversal

武飞扬头像
wenyq7
帮助1

Given the root of an n-ary tree, return the postorder traversal of its nodes' values.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

Example 1:

学新通

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

Example 2:

学新通

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • 0 <= Node.val <= 104
  • The height of the n-ary tree is less than or equal to 1000.

Follow up: Recursive solution is trivial, could you do it iteratively?


递归,也是很简单,只是变成了先遍历children再加root.val。

  1.  
    /*
  2.  
    // Definition for a Node.
  3.  
    class Node {
  4.  
    public int val;
  5.  
    public List<Node> children;
  6.  
     
  7.  
    public Node() {}
  8.  
     
  9.  
    public Node(int _val) {
  10.  
    val = _val;
  11.  
    }
  12.  
     
  13.  
    public Node(int _val, List<Node> _children) {
  14.  
    val = _val;
  15.  
    children = _children;
  16.  
    }
  17.  
    };
  18.  
    */
  19.  
     
  20.  
    class Solution {
  21.  
    public List<Integer> postorder(Node root) {
  22.  
    List<Integer> result = new ArrayList<>();
  23.  
    helper(root, result);
  24.  
    return result;
  25.  
    }
  26.  
     
  27.  
    public void helper(Node root, List<Integer> result) {
  28.  
    if (root == null) {
  29.  
    return;
  30.  
    }
  31.  
    for (Node node : root.children) {
  32.  
    helper(node, result);
  33.  
    }
  34.  
    result.add(root.val);
  35.  
    }
  36.  
    }
学新通

迭代,还是记着Stack Set的方法呢,但是,debug了巨久都没搞出来,大概是seen set的地方弄错了。想着应该要把所有children都seen了才能pop它,但是刚开始想的是seen表示这个node有没有见过,于是要check所有children都seen了还挺麻烦,也不太确定这个思路对不对,就去看了答案。居然几乎没什么人跟我一个思路,看了一个相似的代码,才确认了确实是只有当node没有chidlren或者children全都seen过了才能pop,于是就把seen的含义变成了这个node的所有children是不是都seen过,调整了seen.add的位置,代码就很简洁也一下就没bug了。也算是加深了对postorder iterative的理解。

  1.  
    /*
  2.  
    // Definition for a Node.
  3.  
    class Node {
  4.  
    public int val;
  5.  
    public List<Node> children;
  6.  
     
  7.  
    public Node() {}
  8.  
     
  9.  
    public Node(int _val) {
  10.  
    val = _val;
  11.  
    }
  12.  
     
  13.  
    public Node(int _val, List<Node> _children) {
  14.  
    val = _val;
  15.  
    children = _children;
  16.  
    }
  17.  
    };
  18.  
    */
  19.  
     
  20.  
    class Solution {
  21.  
    public List<Integer> postorder(Node root) {
  22.  
    List<Integer> result = new ArrayList<>();
  23.  
    if (root == null) {
  24.  
    return result;
  25.  
    }
  26.  
    Deque<Node> stack = new ArrayDeque<>();
  27.  
    Set<Node> seen = new HashSet<>(); // this node's children are all seen
  28.  
    stack.push(root);
  29.  
    while (!stack.isEmpty()) {
  30.  
    Node node = stack.peek();
  31.  
    int size = node.children.size();
  32.  
    if (size == 0 || seen.contains(node)) {
  33.  
    node = stack.pop();
  34.  
    result.add(node.val);
  35.  
    } else {
  36.  
    for (int i = size - 1; i >= 0; i--) {
  37.  
    Node child = node.children.get(i);
  38.  
    if (!seen.contains(child)) {
  39.  
    stack.push(child);
  40.  
    }
  41.  
    }
  42.  
    seen.add(node);
  43.  
    }
  44.  
    }
  45.  
    return result;
  46.  
    }
  47.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggiahc
系列文章
更多 icon
同类精品
更多 icon
继续加载