我的任務有問題,我必須計算結構中的最高級別,如下所示:
let node = {
age: 23,
name: "christian",
children: [{
age: 25,
name: "michael",
children: [{
age: 33,
name: "Johann",
children: [{
age: 45,
name: "Christiaaann",
}]
}]
}, {
age: 90,
name: "Monika",
children: [{
age: 10,
name: "WHATEVER",
}]
}]
};
第一個子樹的級別為 3,因為它包含 3 個嵌套的子陣列。正確的級別是 2,因為它包含 2 個嵌套的子陣列。
我試圖遞回地解決它,當然我知道我必須計算每個子樹的級別,然后用條件檢查它是否大于之前的最大值。
我的問題是我不知道在遞回呼叫中在哪里計算它。
這是我的解決方案,到目前為止
let node = {
age: 23,
name: "christian",
children: [{
age: 25,
name: "michael",
children: [{
age: 33,
name: "Johann",
children: [{
age: 45,
name: "Christiaaann",
}]
}]
}, {
age: 90,
name: "Monika",
children: [{
age: 10,
name: "WHATEVER",
}]
}]
};
let level_count;
let max_count = 0;
function level_counter(obj, func) {
level_count = 0;
func(obj);
console.log("INSIDEFUNCTION", level_count);
if (obj.children) {
level_count ;
obj.children.forEach(function(child) {
console.log(child);
level_counter(child, func);
});
if (level_count > max_count) {
max_count = level_count;
}
}
}
function tree_get_levels(root) {
level_counter(root, function(obj) { });
console.log("DOWNONE", level_count);
return 0;
}
let result = tree_get_levels(node);
console.log(result);
uj5u.com熱心網友回復:
我試圖遞回地解決它,當然我知道我必須計算每個子樹的級別,然后用條件檢查它是否大于之前的最大值。
我的問題是我不知道在遞回呼叫中在哪里計算它。
您需要使用遞回呼叫的回傳值,為呼叫它的級別加一。另外,當您嘗試撰寫遞回解決方案時,請始終避免使用全域變數;每個級別的遞回都會看到這些變數的相同值,并且分配給它們將無法正常作業。堅持在遞回函式中定義的變數,并(再次)通過回傳它們來報告值備份堆疊。
請參閱代碼中的行內注釋:
let node = {
age: 23,
name: "christian",
children: [{
age: 25,
name: "michael",
children: [{
age: 33,
name: "Johann",
children: [{
age: 45,
name: "Christiaaann",
}]
}]
}, {
age: 90,
name: "Monika",
children: [{
age: 10,
name: "WHATEVER",
}]
}]
};
function countLevels(obj, func) {
// No levels so far
let levelCount = 0;
func(obj); // Not sure what this function is for
// If there are any children...
if (obj.children) {
// There are, so we've already gone one level down
for (const child of obj.children) {
// Get levels starting at this child, then add one because we've
// already gone one level down
const levelsFromChild = countLevels(child, func) 1;
// Remember it if it's higher than the maximum we've seen
if (levelCount < levelsFromChild) {
levelCount = levelsFromChild;
}
// Or that could be replaced with:
// levelCount = Math.max(levelCount, countLevels(child, func) 1);
}
}
// I added this so you could see the levels count starting from each object
console.log(`${levelCount} level(s) from ${JSON.stringify(obj)}`);
// Return the levels found from this object
return levelCount;
}
// No need for a separate wrapper function, just call the counter directly
let result = countLevels(node, function func(obj) { });
console.log(`Deepest: ${result}`);
.as-console-wrapper {
max-height: 100% !important;
}
uj5u.com熱心網友回復:
你可以試試這個:
function highestDepth(obj) {
let depth = 0;
if (Array.isArray(obj.children) && obj.children.length) {
depth = 1;
let subDepth = 0;
for (const child of obj.children) {
subDepth = Math.max(subDepth, highestDepth(child));
}
depth = subDepth;
}
return depth;
}
console.log(highestDepth(node));
uj5u.com熱心網友回復:
一個簡單的遞回版本首先檢查節點是否具有children. 如果沒有,我們只需回傳0;如果是這樣,我們會遍歷它的每個孩子,并取其中的最大值(或者0如果children陣列為空)并添加1. 它看起來像這樣:
const maxDepth = (node) => 'children' in node
? 1 Math .max (0, ... node .children .map (maxDepth))
: 0
const node = {age: 23, name: "christian", children: [{age: 25, name: "michael", children: [{age: 33, name: "Johann", children: [{age: 45, name: "Christiaaann"}]}]}, {age: 90, name: "Monika", children: [{age: 10, name: "WHATEVER"}]}]}
console .log (maxDepth (node))
作為第0一個引數傳遞給Math .max是必要的,因為Math .max ()(沒有引數)回傳-Infinity。嘗試找出原因是一個有趣的練習。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507823.html
標籤:javascript 递归
