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;
    }
}

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


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

查看所有标签

猜你喜欢:

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

Handbook of Data Structures and Applications

Handbook of Data Structures and Applications

Dinesh P. Mehta / Chapman and Hall/CRC / 2004-10-28 / USD 135.95

In the late sixties, Donald Knuth, winner of the 1974Turing Award, published his landmark book The Art of Computer Programming: Fundamental Algorithms. This book brought to- gether a body of kno......一起来看看 《Handbook of Data Structures and Applications》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具