|
<div class="cnblogs_code">
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意:<span style="color: #000000"> 在返回值的list中,数组长度大的数组靠前)
思路:
1.<span style="color: #000000">二叉树的前序遍历,中左右顺序
2.把目标值target传进去,target-=<span style="color: #000000">val
3.<span style="color: #000000">target为0并且left和right都为null,达到叶结点
4.<span style="color: #000000">函数外部两个数组,list数组存一条路径,listAll数组存所有路径
FindPath(root,<span style="color: #000000">target)
<span style="color: #0000ff">if root==<span style="color: #0000ff">null <span style="color: #0000ff">return<span style="color: #000000"> listAll
<span style="color: #0000ff">list[]=root.<span style="color: #000000">val
target-=root.<span style="color: #000000">val
<span style="color: #0000ff">if target==0 && root->left==<span style="color: #0000ff">null && root->right==<span style="color: #0000ff">null<span style="color: #000000">
listAll[]=<span style="color: #0000ff">list<span style="color: #000000">
FindPath(root->left,<span style="color: #000000">target)
FindPath(root->right,<span style="color: #000000">target)
<span style="color: #008000">//<span style="color: #008000">如果到了这条路径的跟结点,并没有达到目标,就删掉最后的结点,退回上一个结点
<span style="color: #008080">array_pop(<span style="color: #0000ff">list<span style="color: #000000">)
<span style="color: #0000ff">return listAll
= = __construct(->val = <span style="color: #0000ff">function FindPath(<span style="color: #800080">$root,<span style="color: #800080">$target<span style="color: #000000">)
{
<span style="color: #0000ff">static <span style="color: #800080">$list=<span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">static <span style="color: #800080">$listAll=<span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">if(<span style="color: #800080">$root==<span style="color: #0000ff">null<span style="color: #000000">){
<span style="color: #0000ff">return <span style="color: #800080">$listAll<span style="color: #000000">;
}
<span style="color: #800080">$target-=<span style="color: #800080">$root-><span style="color: #000000">val;
<span style="color: #800080">$list[]=<span style="color: #800080">$root-><span style="color: #000000">val;
<span style="color: #0000ff">if(<span style="color: #800080">$target==0 && <span style="color: #800080">$root->left==<span style="color: #0000ff">null && <span style="color: #800080">$root->right==<span style="color: #0000ff">null<span style="color: #000000">){
<span style="color: #800080">$listAll[]=<span style="color: #800080">$list<span style="color: #000000">;
}
FindPath(<span style="color: #800080">$root->left,<span style="color: #800080">$target<span style="color: #000000">);
FindPath(<span style="color: #800080">$root->right,<span style="color: #800080">$target<span style="color: #000000">);
<span style="color: #008080">array_pop(<span style="color: #800080">$list<span style="color: #000000">);
<span style="color: #0000ff">return <span style="color: #800080">$listAll<span style="color: #000000">;
}
<span style="color: #800080">$node10=<span style="color: #0000ff">new TreeNode(10<span style="color: #000000">);
<span style="color: #800080">$node5=<span style="color: #0000ff">new TreeNode(5<span style="color: #000000">);
<span style="color: #800080">$node12=<span style="color: #0000ff">new TreeNode(12<span style="color: #000000">);
<span style="color: #800080">$node4=<span style="color: #0000ff">new TreeNode(4<span style="color: #000000">);
<span style="color: #800080">$node7=<span style="color: #0000ff">new TreeNode(7<span style="color: #000000">);
<span style="color: #800080">$node10->left=<span style="color: #800080">$node5<span style="color: #000000">;
<span style="color: #800080">$node10->right=<span style="color: #800080">$node12<span style="color: #000000">;
<span style="color: #800080">$node5->left=<span style="color: #800080">$node4<span style="color: #000000">;
<span style="color: #800080">$node5->left=<span style="color: #800080">$node7<span style="color: #000000">;
<span style="color: #800080">$tree=<span style="color: #800080">$node10<span style="color: #000000">;
<span style="color: #800080">$res=FindPath(<span style="color: #800080">$tree,22<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$res);
<span style="color: #008000">/<span style="color: #008000">class TreeNode{
var $val;
var $left = NULL;
var $right = NULL;
function __construct($val){
$this->val = $val;
}
}<span style="color: #008000">/
<span style="color: #0000ff">function FindPath(<span style="color: #800080">$root,<span style="color: #800080">$target<span style="color: #000000">)
{
<span style="color: #800080">$list=<span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #800080">$listAll=<span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #800080">$res=dfs(<span style="color: #800080">$root,<span style="color: #800080">$target,<span style="color: #800080">$list,<span style="color: #800080">$listAll<span style="color: #000000">);
<span style="color: #0000ff">return <span style="color: #800080">$res<span style="color: #000000">;
}
<span style="color: #0000ff">function dfs(<span style="color: #800080">$root,&<span style="color: #800080">$list,&<span style="color: #800080">$listAll<span style="color: #000000">)
{
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$root</span>==<span style="color: #0000ff">null</span><span style="color: #000000">){
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$listAll</span><span style="color: #000000">;
}
</span><span style="color: #800080">$target</span>-=<span style="color: #800080">$root</span>-><span style="color: #000000">val;
</span><span style="color: #800080">$list</span>[]=<span style="color: #800080">$root</span>-><span style="color: #000000">val;
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$target</span>==0 && <span style="color: #800080">$root</span>->left==<span style="color: #0000ff">null</span> && <span style="color: #800080">$root</span>->right==<span style="color: #0000ff">null</span><span style="color: #000000">){
</span><span style="color: #800080">$listAll</span>[]=<span style="color: #800080">$list</span><span style="color: #000000">;
}
dfs(</span><span style="color: #800080">$root</span>->left,<span style="color: #800080">$listAll</span><span style="color: #000000">);
dfs(</span><span style="color: #800080">$root</span>->right,<span style="color: #800080">$listAll</span><span style="color: #000000">);
</span><span style="color: #008080">array_pop</span>(<span style="color: #800080">$list</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$listAll</span><span style="color: #000000">;
} (编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|