当前位置: 首页 > 后端技术 > PHP

LeetcodePHP题解--D44590.N-aryTreePostorderTraversal

时间:2023-03-29 21:54:17 PHP

D44590.N-aryTreePostorderTraversal题目链接590.N-aryTreePostorderTraversal题目分析后序遍历,这题也是比较基础的题目.思路是先遍历子节点,再遍历根节点。Node.class节点的最终代码$children函数__construct($val,$children){$this->val=$val;$this->children=$children;}}*/class解决方案{public$val=[];/***@paramNode$root*@returnInteger[]*/functionpostorder($root){if(!$root){return$this->val;}foreach($root->childrenas$child){$this->postorder($child);}$this->val[]=$root->val;返回$this->val;}}如果您觉得本文对您有用,欢迎用爱心募捐。