且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JavaScript中搜索二叉树

更新时间:2021-11-22 23:03:10

在这种特定情况下,您可能需要使用以下代码:

In this specific case you'd might want to use this:

var baseObject = {
    "value": 5,
    "children": [{
        "value": 18,
        "children": [{
            "value": 27,
            "children": []
        }, {
            "value": 4,
            "children": []
        }]
    }, {
        "value": 2,
        "children": []
    }]
};

function getHighestValue(obj) {
    var res = obj.value;
    for(var i in obj.children) {
        res = Math.max(res, getHighestValue(obj.children[i]));
    }
    return res;
}

alert(getHighestValue(baseObject));

http://jsfiddle.net/qc9R4/1/