国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 589. N-ary Tree Preorder Traversal (vs.

array_huang / 3177人閱讀

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:

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

Solution (Iteration)

Using stack, push the child from the end of list

class Solution {
    public List preorder(Node root) {
        List res = new ArrayList<>();
        if (root == null) return res;
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            res.add(cur.val);
            for (int i = cur.children.size()-1; i >= 0; i--) stack.push(cur.children.get(i));
        }
        return res;
    }
}
Solution (Recursion)
/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

    public Node(int _val,List _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
    public List preorder(Node root) {
        List res = new ArrayList<>();
        helper(root, res);
        return res;
    }
    private void helper(Node root, List res) {
        if (root == null) return;
        res.add(root.val);
        for (Node node: root.children) {
            helper(node, res);
        }
    }
}
144. Binary Tree Preorder Traversal

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

Example:

Input: [1,null,2,3]

   1
    
     2
    /
   3

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

Solution (Iteration)

Use stack, first push node.right, then push node.left

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/77377.html

相關(guān)文章

  • Leetcode PHP題解--D43 589. N-ary Tree Preorder Trave

    摘要:題目鏈接題目分析維數(shù)組的先序遍歷。這題也不想多說(shuō)什么了。是比較基礎(chǔ)的題目了。先序就是先根后子而已。思路在遍歷子節(jié)點(diǎn)之前,先保存當(dāng)前節(jié)點(diǎn)的信息。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 D43 589. N-ary Tree Preorder Traversal 題目鏈接 589. N-ary Tree Preorder Traversal 題目分析 N維數(shù)組的先序遍歷。 這題也...

    junbaor 評(píng)論0 收藏0
  • [LeetCode] 590. N-ary Tree Postorder Traversal (vs

    摘要:按順序放入,正好方面是從到,順序方面是從最右到最左,因?yàn)槭窍热牒蟪觥_@樣最后一下就是先左后右,先子后根。 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-ar...

    sydMobile 評(píng)論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 評(píng)論0 收藏0
  • leetcode429. N-ary Tree Level Order Traversal

    摘要:題目要求對(duì)叉樹(shù)進(jìn)行水平遍歷,并輸出每一行遍歷的結(jié)果。因此無(wú)需再用隊(duì)列來(lái)額外存儲(chǔ)每一行的水平遍歷,可以直接通過(guò)遞歸將遍歷結(jié)果插入到相應(yīng)行的結(jié)果集中。 題目要求 Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level)...

    tomlingtm 評(píng)論0 收藏0
  • Leetcode PHP題解--D55 429. N-ary Tree Level Order Tr

    摘要:題目鏈接題目分析按層遍歷叉樹(shù)。思路以層數(shù)為鍵,塞入當(dāng)前節(jié)點(diǎn)的值。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 D55 429. N-ary Tree Level Order Traversal 題目鏈接 429. N-ary Tree Level Order Traversal 題目分析 按層遍歷N叉樹(shù)。 思路 以層數(shù)為鍵,塞入當(dāng)前節(jié)點(diǎn)的值。 遞歸遍歷即可。 最終代碼

    libxd 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<