leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历

递归方法

C++代码:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4 public:
 5     int val;
 6     vector<Node*> children;
 7
 8     Node() {}
 9
10     Node(int _val, vector<Node*> _children) {
11         val = _val;
12         children = _children;
13     }
14 };
15 */
16 class Solution {
17 public:
18     vector<int> postorder(Node* root) {
19         vector<int>res;
20         post(root,res);
21         return res;
22     }
23     void post(Node*root, vector<int> &res){
24         if(root==NULL) return;
25         for(int i=0;i<root->children.size();i++){
26             post(root->children[i],res);
27         }
28         res.push_back(root->val);
29     }
30 };

原文地址:https://www.cnblogs.com/joelwang/p/10336348.html

时间: 2024-07-31 19:54:38

leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历的相关文章

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversa

POJ 2255 Tree Recovery &amp;&amp; Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

链接:poj.org/problem?id=2255 题意: 分别给你一个二叉树的前序遍历序列和中序遍历序列,让你给出这个二叉树的后序遍历序列. 思路: 对于二叉树的三种遍历方式,都可以使用递归来实现,那么也一定可以使用递归来拆解,以达到从遍历序列确定二叉树具体结构的目的.对于前序遍历来说,第一个字母一定是根,并且在序列中根的左子树包含的点一定出现在根的右子树的前面.对于中序遍历序列来说,根前面出现的字母所代表的点一定出现在左子树中,根后面出现的字母所代表的点一定出现在右子树中.在根据前序与中序

590. N叉树的后序遍历

给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? /* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) { val = _v

leetcode || 145、Binary Tree Postorder Traversal

problem: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归后续遍历二叉树

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其中序遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [1, 3, 2] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursi