Leetcode PHP题解--D43 589. N-ary Tree Preorder Traversal

栏目: PHP · 发布时间: 6年前

内容简介: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;
    }
}

若觉得本文章对你有用,欢迎用 爱发电 资助。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Design in a Nutshell

Web Design in a Nutshell

Jennifer Niederst / O'Reilly Media, Inc. / 2006-02-21 / USD 34.99

Are you still designing web sites like it's 1999? If so, you're in for a surprise. Since the last edition of this book appeared five years ago, there has been a major climate change with regard to web......一起来看看 《Web Design in a Nutshell》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试