Home  >  Article  >  Backend Development  >  How to implement a path that sums to a certain value in a binary tree in PHP (code)

How to implement a path that sums to a certain value in a binary tree in PHP (code)

不言
不言forward
2018-10-11 15:45:312136browse

The content of this article is about how PHP implements the path (code) that neutralizes a binary tree to a certain value. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

The path where the sum of the binary tree is a certain value:

Input the following node of a binary tree and an integer, and print out all the nodes in the binary tree whose sum is the input integer. path. A path is defined as a path starting from the root node of the tree and going down to the nodes passing through the leaf nodes. (Note: In the list of return values, the array with the larger array length is placed first)

Idea:

1. Preorder traversal of the binary tree, left and right order

2 , pass the target value target in, target-=val

3, target is 0 and left and right are both null, reach the leaf node

4, two arrays outside the function, list The array stores a path, and the listAll array stores all paths

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)
    //如果到了这条路径的跟结点,并没有达到目标,就删掉最后的结点,退回上一个结点
    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;
}

The above is the detailed content of How to implement a path that sums to a certain value in a binary tree in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete