内容简介:N维数组的先序遍历。这题也不想多说什么了。是比较基础的题目了。
589. N-ary Tree Preorder Traversal
题目链接
589. N-ary Tree Preorder Traversal
题目分析
N维数组的先序遍历。
这题也不想多说什么了。是比较基础的题目了。
先序就是先根后子而已。没什么难的。
思路
在遍历子节点之前,先保存当前节点的信息。
最终代码
<?php
class Solution {
public $val = [];
function preorder($root) {
if(!$root){
return $this->val;
}
$this->val[] = $root->val;
foreach($root->children as $child){
$this->preorder($child);
}
return $this->val;
}
}
若觉得本文章对你有用,欢迎用 爱发电 资助。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Sovereign Individual
James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00
Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!