<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script>
const sourceTree = {
id: "i1",
value: 17,
left: {
id: "i3",
value: 83,
left: {
id: "i4",
value: 101,
},
right: {
id: "i9",
value: 22,
},
},
right: {
id: "i11",
value: 26,
},
};
var sz = [];
function findMaxNode(tree) {
for (let key in tree) {
if (tree[key] instanceof Object) {
for (let a in tree[key]) {
if (tree[key][a] instanceof Object) {
for (let b in tree[key][a]) {
let vote = {};
if (b == "id") {
vote.id = tree[key][a][b];
} else {
vote.value = tree[key][a][b];
}
sz.push(vote);
}
} else {
console.log(a + "---" + tree[key][a]);
let vote = {};
if (a == "id") {
vote.id = tree[key][a];
} else {
vote.value = tree[key][a];
}
sz.push(vote);
}
}
} else {
console.log(key + "---" + tree[key]);
let vote = {};
if (key == "id") {
vote.id = tree[key];
} else {
vote.value = tree[key];
}
sz.push(vote);
}
}
}
findMaxNode(sourceTree);
console.log(sz);
sz2 = [];
for (i = 1; i < sz.length; i = i + 2) {
// console.log("haha");
console.log(sz[i].value);
sz2.push(sz[i].value);
}
console.log(Math.max(...sz2));
console.log(sz2.indexOf(101));
var sy = sz2.indexOf(Math.max(...sz2)) * 2 + 1;
console.log(sy);
console.log(sz[sy - 1]);
console.log(sz[sy]);
var hb = sz[sy - 1];
hb["value"] = sz[sy].value;
console.log(hb);
</script>
</head>
<body></body>
</html>
从一个树状数据结构中,找出值最大的一个节点
Guess you like
Origin blog.csdn.net/liulang68/article/details/121293551
Recommended
Ranking