佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > php實(shí)現(xiàn)二叉樹(shù)中和為某一值的路徑方法

php實(shí)現(xiàn)二叉樹(shù)中和為某一值的路徑方法

熱門(mén)標(biāo)簽:銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器 阿里云 電子圍欄 服務(wù)器配置

二叉樹(shù)中和為某一值的路徑:

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

思路:

1、二叉樹(shù)的前序遍歷,中左右順序

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),并沒(méi)有達(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;

}

以上就是本次內(nèi)容的全部實(shí)例代碼,大家可以本次測(cè)試一下,感謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • PHP排序二叉樹(shù)基本功能實(shí)現(xiàn)方法示例
  • PHP實(shí)現(xiàn)二叉樹(shù)深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次)實(shí)例詳解
  • PHP實(shí)現(xiàn)從上往下打印二叉樹(shù)的方法
  • PHP獲取二叉樹(shù)鏡像的方法
  • PHP實(shí)現(xiàn)按之字形順序打印二叉樹(shù)的方法
  • PHP基于非遞歸算法實(shí)現(xiàn)先序、中序及后序遍歷二叉樹(shù)操作示例
  • PHP實(shí)現(xiàn)判斷二叉樹(shù)是否對(duì)稱(chēng)的方法
  • PHP實(shí)現(xiàn)繪制二叉樹(shù)圖形顯示功能詳解【包括二叉搜索樹(shù)、平衡樹(shù)及紅黑樹(shù)】
  • PHP完全二叉樹(shù)定義與實(shí)現(xiàn)方法示例

標(biāo)簽:衡水 大理 萍鄉(xiāng) 蚌埠 棗莊 衢州 廣元 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實(shí)現(xiàn)二叉樹(shù)中和為某一值的路徑方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    永春县| 吉安县| 武胜县| 寿宁县| 涪陵区| 灌云县| 嘉峪关市| 大埔县| 涞源县| 绥德县| 泰安市| 安乡县| 金山区| 邢台市| 永新县| 南溪县| 淳化县| 桂东县| 莱州市| 遵义县| 鹿泉市| 五常市| 韶关市| 新巴尔虎右旗| 集安市| 鄱阳县| 台州市| 石屏县| 德州市| 英德市| 哈巴河县| 桂林市| 东辽县| 柯坪县| 荆州市| 尚义县| 西充县| 虞城县| 北辰区| 乾安县| 健康|