欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂PHP培訓(xùn)簡(jiǎn)述PHP實(shí)現(xiàn)二叉樹中和為某一值的路徑方法

2019-05-15 11:17:39 4896瀏覽

本篇文章扣丁學(xué)堂PHP培訓(xùn)小編給讀者們分享一下PHP實(shí)現(xiàn)二叉樹中和為某一值的路徑方法,對(duì)PHP開發(fā)技術(shù)感興趣的小伙伴就隨小編來了解一下吧,希望對(duì)小伙伴們有幫助。



扣丁學(xué)堂PHP培訓(xùn)簡(jiǎn)述PHP實(shí)現(xiàn)二叉樹中和為某一值的路徑方法



二叉樹中和為某一值的路徑:


輸入一顆二叉樹的跟節(jié)點(diǎn)和一個(gè)整數(shù),打印出二叉樹中結(jié)點(diǎn)值的和為輸入整數(shù)的所有路徑。路徑定義為從樹的根結(jié)點(diǎn)開始往下一直到葉結(jié)點(diǎn)所經(jīng)過的結(jié)點(diǎn)形成一條路徑。(注意: 在返回值的list中,數(shù)組長(zhǎng)度大的數(shù)組靠前)



路:


1、二叉樹的前序遍歷,中左右順序

2、把目標(biāo)值target傳進(jìn)去,target-=val

3、target為0并且left和right都為null,達(dá)到葉結(jié)點(diǎn)

4、函數(shù)外部?jī)蓚€(gè)數(shù)組,list數(shù)組存一條路徑,listAll數(shù)組存所有路徑


FindPath(root,target)

  if root==null return listAll

  list[]=root.val

  target-=root.val

  if target==0 && root->left==null && root->right==null

    listAll[]=list

  FindPath(root->left,target)

  FindPath(root->right,target)

  //如果到了這條路徑的跟結(jié)點(diǎn),并沒有達(dá)到目標(biāo),就刪掉最后的結(jié)點(diǎn),退回上一個(gè)結(jié)點(diǎn)

  array_pop(list)

  return listAll
<?php

class TreeNode{

  var $val;

  var $left = NULL;

  var $right = NULL;

  function __construct($val){

    $this->val = $val;

  }  

}

 

function FindPath($root,$target)

{

    static $list=array();

    static $listAll=array();

    if($root==null){

        return $listAll;

    }  

    $target-=$root->val;

    $list[]=$root->val;

    if($target==0 && $root->left==null && $root->right==null){

        $listAll[]=$list;

    }  

    FindPath($root->left,$target);

    FindPath($root->right,$target);

    array_pop($list);

    return $listAll;

}

 

$node10=new TreeNode(10);

$node5=new TreeNode(5);

$node12=new TreeNode(12);

$node4=new TreeNode(4);

$node7=new TreeNode(7);

 

$node10->left=$node5;

$node10->right=$node12;

$node5->left=$node4;

$node5->left=$node7;

 

$tree=$node10;

 

$res=FindPath($tree,22);

var_dump($res);
<?php

/*class TreeNode{

  var $val;

  var $left = NULL;

  var $right = NULL;

  function __construct($val){

    $this->val = $val;

  }

}*/

function FindPath($root,$target)

{

  $list=array();

  $listAll=array();

  $res=dfs($root,$target,$list,$listAll);

  return $res;

}

 

function dfs($root,$target,&$list,&$listAll)

{

 

    if($root==null){

        return $listAll;

    }  

    $target-=$root->val;

    $list[]=$root->val;

    if($target==0 && $root->left==null && $root->right==null){

         

        $listAll[]=$list;

    }  

    dfs($root->left,$target,$list,$listAll);

    dfs($root->right,$target,$list,$listAll);

    array_pop($list);

    return $listAll;

}

想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂PHP技術(shù)交流群:374332265。


扣丁學(xué)堂微信公眾號(hào)                              HTML5前端開發(fā)VIP免費(fèi)公開課

     【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】           【掃碼進(jìn)入HTML5前端開發(fā)VIP免費(fèi)公開課



查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>


標(biāo)簽: PHP培訓(xùn) PHP基礎(chǔ)教程 PHP學(xué)習(xí)視頻 PHP教學(xué)視頻 PHP入門教程 PHP教程視頻 PHP在線學(xué)習(xí) PHP在線視頻 PHP在線教程 扣丁學(xué)堂PHP培訓(xùn)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部