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?
Using stack, push the child from the end of list
class Solution { public ListSolution (Recursion)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; } }
/* // Definition for a Node. class Node { public int val; public List144. Binary Tree Preorder Traversalchildren; 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); } } }
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?
Use stack, first push node.right, then push node.left
class Solution { public ListSolution (Recursion)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; } }
class Solution { public ListpreorderTraversal(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); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/77377.html
摘要:题目链接题目分析维数组的先序遍历。这题也不想多说什么了。是比较基础的题目了。先序就是先根后子而已。思路在遍历子节点之前,先保存当前节点的信息。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D43 589. N-ary Tree Preorder Traversal 题目链接 589. N-ary Tree Preorder Traversal 题目分析 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...
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...
摘要:题目要求对叉树进行水平遍历,并输出每一行遍历的结果。因此无需再用队列来额外存储每一行的水平遍历,可以直接通过递归将遍历结果插入到相应行的结果集中。 题目要求 Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level)...
摘要:题目链接题目分析按层遍历叉树。思路以层数为键,塞入当前节点的值。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D55 429. N-ary Tree Level Order Traversal 题目链接 429. N-ary Tree Level Order Traversal 题目分析 按层遍历N叉树。 思路 以层数为键,塞入当前节点的值。 递归遍历即可。 最终代码
阅读 3405·2021-09-26 09:46
阅读 2742·2021-09-13 10:23
阅读 3465·2021-09-07 10:24
阅读 2375·2019-08-29 13:20
阅读 2898·2019-08-28 17:57
阅读 3053·2019-08-26 13:27
阅读 1162·2019-08-26 12:09
阅读 484·2019-08-26 10:27