<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title></title> </head> <body> <script> var tree= { value: 'd', left: { value: 'o', left: { value: 'g', left: { value: 'n', left: { value: 'o', left: { value: 'd', left: {value: 'e'} }, }, }, right: {value: 'a'} }, right: { value: 'r', left: {value: 'n'}, right: {value: '2'} } }, right: { value: 'n', left: { value: 'u', left: {value: '0'}, right: { value: '1' } }, right: { value: 'i', left: {value: '6'}, right: { value: 'n' } } } }; var dlrAry=[]; function dlr(node){ if (node) { dlrAry.push(node.value); dlr(node.left); dlr(node.right); } } dlr(tree); alert(dlrAry.join('')) //先序遍历(dlr) var dlrAry=[]; function dlr(node){ if(node){ dlrAry.push(node.value); dlr(node.left); dlr(node.right); } } dlr(tree); console.log('先序遍历:'+dlrAry.join('')); //中序遍历(ldr) var ldrAry=[]; function ldr(node){ if(node){ ldr(node.left); ldrAry.push(node.value); ldr(node.right); } } ldr(tree); console.log('中序遍历:'+ldrAry.join('')); //后序遍历(lrd) var lrdAry=[]; function lrd(node){ if(node){ lrd(node.left); lrd(node.right); lrdAry.push(node.value); } } lrd(tree); console.log('后序遍历:'+lrdAry.join('')); </script> </head> <body> </body> </html>