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 题目描述 给定一 ...
随机推荐
- python中的import一个注意事项
import math def foo(): import math x = math.pi # 如果math在下面import会出错,因为import是个写的过程(添加到sys.modules中), ...
- ZC_注意点
1. domain类 里面的 属性的类型,一般都是用 包装类 2. 使用 "Hibernate Reverse Engineering ..." 来进行自动生成domain类和?? ...
- 计时器(C#)
很多项目要用到计时器,我就自己包装了一个,倒计时还没加,有时间再加上吧.持续更新 using UnityEngine; using UnityEngine.UI; /// <summary> ...
- WPF TextBox 只能输入数字键
<Grid> <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewT ...
- Mac开机启动
1. Finder打开资源库的LaunchAgents目录. 打开Finder,按⇧⌘G,输入 /Library/LaunchAgents/ 以及 ~/Library/LaunchAgents/ 2. ...
- 为mac终端添加tree命令
原文:http://superuser.com/questions/359723/mac-os-x-equivalent-of-the-ubuntu-tree-command/ 整理步骤如下: $ t ...
- 51nod 1281 二分
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1281 隐藏话题 1281 山峰和旗子 题目来源: Codility 基准 ...
- 使文字出现波纹效果--第三方开源--Titanic
下载地址:https://github.com/RomainPiel/Titanic 使用的时候直接将代码复制过来即可(注意res文件下有张波浪图也要一起复制) xml代码: <com.roma ...
- SDK中常用的工具
Android SDK包含了各种各样的定制工具,简介如下: 一.Android模拟器(Android Emulator )它是在你的计算机上运行的一个虚拟移动设备.你可以使用模拟器来在一个实际的And ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...