leetcode589
树的先序遍历,使用递归实现。
class Solution {
public:
vector<Node> Tree;
void preTree(Node node)
{
Tree.push_back(Node(node.val, node.children));
for (auto n : node.children)
{
Node node;
node = Node(n->val, n->children);
preTree(node);
}
}
vector<int> preorder(Node* root) {
vector<int> V;
if (root != NULL)
{
preTree(*root);
for (auto n : Tree)
{
V.push_back(n.val);
}
}
return V;
}
};
leetcode589的更多相关文章
- (N叉树 递归) leetcode589. N-ary Tree Preorder Traversal
Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary ...
- leetcode589. N-ary Tree Preorder Traversal
python 版: class Solution(object): def preorder(self, root): ret, q = [], root and [root] while q: no ...
- Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历
给定一个 N 叉树,返回其节点值的前序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...
- LeetCode589. N叉树的前序遍历
题目 法一.递归 1 class Solution { 2 public: 3 vector<int>ans; 4 void dfs(Node* root){ 5 if(root!=NUL ...
- [树]LeetCode589 N叉树的前序遍历
LeetCode N叉树的前序遍历 前言:树的前中后序遍历已经是很经典的题目的,要么递归要么迭代,不过还是比较习惯于递归的写法 TITLE 给定一个 n 叉树的根节点 root ,返回 其节点值的 前 ...
- (N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal
Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...
- LeetCode解题思路
刷完题后,看一下其他人的solution,受益匪浅. 可以按不同的topic刷题,比如数组.字符串.集合.链表等等.先做十道数组的题,接着再做十道链表的题. 刷题,最主要的是,学习思路. 多刷几遍.挑 ...
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
随机推荐
- alibaba的JSON.toString会把值为null的字段去掉,谨记
alibaba的JSON.toString会把值为null的字段去掉,谨记 Map<String,Object> map = new HashMap<>(); map.put( ...
- [thinkphp使用phpspreadsheet时出现]Cannot redeclare xxxxxx() (previously declared in C:\WWW\xxx.xxx:xxx)
[thinkphp使用phpspreadsheet时出现]Cannot redeclare xxxxxx() (previously declared in C:\WWW\xxx.xxx:xxx) 一 ...
- 解决:AttributeError: module 'requests' has no attribute 'get'”
今天学习Requests库,当用pip install requests安装后,写了一段代码报错:AttributeError: module 'requests' has no attribute ...
- 关于C++中的pow小记(转)
昨天在敲一个数位DP的问题,但是用到了这个坑D的问题,找了半天错,还以为又是什么奇怪的算法,结果发现思路一致,然后自己各种YY修改,最后不得不和正确答案比对,但是最后发现标准答案和自己的想法几乎一模一 ...
- Asp.net 异步调用WebService
//服务代码 [WebMethod] public string Test(int sleepTimes, int val) { Thread.Sleep(sleepTimes); var log = ...
- Hive数据导入——数据存储在Hadoop分布式文件系统中,往Hive表里面导入数据只是简单的将数据移动到表所在的目录中!
转自:http://blog.csdn.net/lifuxiangcaohui/article/details/40588929 Hive是基于Hadoop分布式文件系统的,它的数据存储在Hadoop ...
- Nodejs调试技术总结
调试技术与开发技术构成了软件开发的基石.目前Nodejs作为新型的Web Server开发栈倍受开发者关注.总的来说Nodejs的应用程序主要有两部分:JavaScript编写的js模块和C语言编译的 ...
- c++primer 第五章编程练习答案
5.9.1 #include<iostream> int main() { using namespace std; ; cout << "input first i ...
- Spring_总结_03_装配Bean(一)_自动装配
一.前言 本文承接上一节:Spring_总结_02_依赖注入 在上一节我们了解到依赖注入的实质就是装配. 这一节我们来学习下装配Bean的相关知识. 二.Bean的装配机制 1.三种装配机制 Spri ...
- charles mock数据时解决乱码问题
1.新建文件,不要后缀名 2.下载Notepad++ 软件,使用该软件打开文件:设置:格式---->以UTF-8无BOM格式编码 3.正常在文件中写入数据,maplocal 就可以了