资讯专栏INFORMATION COLUMN

[LeetCode] 590. N-ary Tree Postorder Traversal (vs

sydMobile / 2258人阅读

摘要:按顺序放入,正好方面是从到,顺序方面是从最右到最左,因为是先入后出。这样最后一下就是先左后右,先子后根。

590. N-ary Tree Postorder Traversal Problem

Given an n-ary tree, return the postorder traversal of its nodes" values.
For example, given a 3-ary tree:

Return its postorder traversal as: [5,6,3,2,4,1].
Note: Recursive solution is trivial, could you do it iteratively?

Solution (Recursion)
class Solution {
    public List postorder(Node root) {
        List res = new ArrayList<>();
        helper(root, res);
        return res;
    }
    private void helper(Node root, List res) {
        if (root == null) return;
        if (root.children != null) {
            for (Node child: root.children) {
                helper(child, res);
            }
        }
        res.add(root.val);
    }
}
Solution (Iteration)

按顺序放入stack,正好level方面是从root到leaves,顺序方面是从最右到最左,因为stack是先入后出。这样最后reverse一下就是先左后右,先子后根。
巧妙。

class Solution {
    public List postorder(Node root) {
        List res = new ArrayList<>();
        if (root == null) return res;
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            res.add(node.val);
            for (Node child: node.children) {
                stack.push(child);
            }
        }
        Collections.reverse(res);
        return res;
    }
}
145. Binary Tree Postorder Traversal

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

Example:

Input: [1,null,2,3]

   1
    
     2
    /
   3

Output: [3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?

Solution (Iteration)
class Solution {
    public List postorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        if (root == null) return res;
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.left != null) stack.push(node.left);
            if (node.right != null) stack.push(node.right);
        }
        Collections.reverse(res);
        return res;
    }
}
Solution (Recursion)
class Solution {
    public List postorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        helper(root, res);
        return res;
    }
    private void helper(TreeNode node, List res) {
        if (node == null) return;
        if (node.left != null) helper(node.left, res);
        if (node.right != null) helper(node.right, res);
        res.add(node.val);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77376.html

相关文章

  • Leetcode PHP题解--D44 590. N-ary Tree Postorder Trav

    摘要:题目链接题目分析后序遍历,这题也是比较基础的题目了。思路先遍历子节点,再遍历根节点。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D44 590. N-ary Tree Postorder Traversal 题目链接 590. N-ary Tree Postorder Traversal 题目分析 后序遍历,这题也是比较基础的题目了。 思路 先遍历子节点,再遍历根节点。 最终代码...

    Songlcy 评论0 收藏0
  • [LeetCode] 589. N-ary Tree Preorder Traversal (vs.

    589. N-ary Tree Preorder Traversal Given an n-ary tree, return the preorder traversal of its nodes values.For example, given a 3-ary tree:showImg(https://segmentfault.com/img/bVbhKkv?w=781&h=502);Retu...

    array_huang 评论0 收藏0
  • [LeetCode] 429. N-ary Tree Level Order Traversal (

    429. N-ary Tree Level Order Traversal Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level). For example, given a 3-ary tree:showImg(https...

    LiangJ 评论0 收藏0
  • leetcode429. N-ary Tree Level Order Traversal

    摘要:题目要求对叉树进行水平遍历,并输出每一行遍历的结果。因此无需再用队列来额外存储每一行的水平遍历,可以直接通过递归将遍历结果插入到相应行的结果集中。 题目要求 Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level)...

    tomlingtm 评论0 收藏0
  • Leetcode PHP题解--D55 429. N-ary Tree Level Order Tr

    摘要:题目链接题目分析按层遍历叉树。思路以层数为键,塞入当前节点的值。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D55 429. N-ary Tree Level Order Traversal 题目链接 429. N-ary Tree Level Order Traversal 题目分析 按层遍历N叉树。 思路 以层数为键,塞入当前节点的值。 递归遍历即可。 最终代码

    libxd 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<