function maxDepth(root){
root = {
value: 1,
left: {
value: 2,
left: {
value: 4
}
},
right: {
value: 3,
left: {
value: 5,
left: {
value: 7
},
right: {
value: 8
}
},
right: {
value: 6
}
}
}
let leftNode = [];
let result = [];
leftNode.push(root)
while(root){
if( root.left){
leftNode.push(root.left);
}
root = root.left;
}
while( leftNode.length ){
root = leftNode.pop();
let rightNode = root.right;
if (rightNode) {
leftNode.push( rightNode );
while( rightNode.left ){
rightNode = rightNode.left;
leftNode.push( rightNode )
}
}
result.push( root.value)
}
return result;
}
二叉树的中序遍历
最新推荐文章于 2025-01-13 19:47:10 发布
1049

被折叠的 条评论
为什么被折叠?



