目錄
一、商品無限極分類
二、按照樹狀返回父級列表和子集列表
三、判斷兩棵樹是否相同
四、計算樹的高度 id
pidpath
name
0-1**
0-2**
0-1-3
休閒**
0-1-4
潮流**
0-1-5
品牌**
$list = [
['id' => 1, 'pid' => 0, 'path' => '0-1', 'name' => '**'],
['id' => 2, 'pid' => 0, 'path' => '0-2', 'name' => '**'],
['id' => 3, 'pid' => 1, 'path' => '0-1-3', 'name' => '休閒**'],
['id' => 4, 'pid' => 1, 'path' => '0-1-4', 'name' => '潮流**'],
['id' => 5, 'pid' => 1, 'path' => '0-1-5', 'name' => '品牌**']
];function getchild($list)
echo json_encode( getchild($list) );
/*[ ,,]
},]*/
function find_child($ar, $id='self_id', $pid='parent_id')
} return $t;
}function find_parent($ar, $id='id', $pid='parent_id')
} return $t;
/*方法2:
foreach($ar as $v) $t[$v[$id]] = $v;
foreach ($t as $item)
}*/}function find_child_ids($all_data, $cur_id, &$res)
foreach ($childs as $child)
}}function find_parent_ids($all_data, $cur_id, &$res)
foreach($parents as $parent)
}}$data = array(
array('id'=>1, 'pid'=>0, 'name'=>'祖父'),
array('id'=>2, 'pid'=>1, 'name'=>'父親'),
array('id'=>3, 'pid'=>1, 'name'=>'叔伯'),
array('id'=>4, 'pid'=>2, 'name'=>'自己'),
array('id'=>5, 'pid'=>4, 'name'=>'兒子'),
);
$all_child = find_child($data, 'id', 'pid');
$all_parent = find_parent($data, 'id', 'pid');
$child_ids=; $cur_id="1";
find_child_ids($all_child, $cur_id, $child_ids);
$parent_ids=; $cur_id="5";
find_parent_ids($all_parent, $cur_id, $parent_ids);
echo json_encode([
'all_child' => $all_child,
'child_ids' => $child_ids,
'all_parent' => $all_parent,
'parent_ids' => $parent_ids
]);
/*]}
]},]}
]},
"1":]}
]},]},
"2":]}
]},
"3":,
"4":]},
"5":
},"child_ids":[
2,3,4,5
],"all_parent":,
"2":]},
"3":]},
"4":]}
]},
"5":]}
]}]}
},"parent_ids":[
4,2,1]
}*/
/*
思路:判斷兩棵樹是否相同
def issametree(p, q):
if p == none and q == none:
return true
elif p and q :
return p.val == q.val and issametree(p.child,q.child)
else :
return false
*/function issametree($a, $b)
if($a['id']!=$b['id'] || $a['name']!=$b['name'] || $a['pid']!=$b['pid'])
//step2:判斷孩子是否一樣
if( ( isset($a['child']) && !isset($a['child']) )
|| ( !isset($a['child']) && isset($a['child']) )
|| ( isset($a['child']) && isset($a['child']) && count($a['child'])!=count($b['child']) )
) $achild = $bchild=;
foreach($a['child'] as $v1) $achild[$v1['id']]=$v1;
foreach($b['child'] as $v2) $bchild[$v2['id']]=$v2;
foreach($achild as $id => $v)
//step3:走到最後,返回true
return true;
}
$a = ']}
]},
]}';$b = ',]}
]},
]}';$a=json_decode($a, true); $b=json_decode($b, true);
$res = issametree($a, $b);
$res2 = $res==true ? ('true') : ('false');
echo $res2;die;
內容不一樣[[4,4],["自己","自己22"],[2,2]]
false
/* 思路
def maxdepth(root):
if not root: return 0
return max(maxdepth(root.left), maxdepth(root.right)) + 1
*/
excel巨集 終極版
private sub commandbutton1 click sheet1 q20.list sheet2 q20trim.list sheet3 源資料表 副本 新建sheet4 批量合成 刪除空行 變成m 最終資料存於sheet3列pqr 批量合成 dim s as integer s te...
頁面傳參終極版
頁面傳引數是一種比較常見的業務需求,根據實現原理及適用環境可以分為兩大類。在普通瀏覽器端常用的方法有如下幾種 1.利用url傳參 在頁面跳轉的時候通過設定window.location.href新增引數,在接收引數的頁面通過window.location.search獲取引數字串。傳送引數的頁面 w...
kewin 接小球終極版
import pygame import random 1.loading 初始化 pygame.init 2.視窗 dis分開 play玩 展示 screen width 960screen height 540screen pygame.display.set mode screen width...