//中序遍历:左,中,右
var inorderTraversal = function(root) {
var arr = []
function order(node){
if(node != null){
if(node.left != null){
order(node.left)
}
arr.push(node.val)
if(node.right != null){
order(node.right)
}
}
}
order(root)
return arr
};
// 先序遍历的递归算法:
var preOrder = function (node) {
if (node) {
console.log(node.value);
preOrder(node.left);
preOrder(node.right);
}
}
// 中序遍历的递归算法:
var inOrder = function (node) {
if (node) {
inOrder(node.left);
console.log(node.value);
inOrder(node.right);
}
}
// 后序遍历的递归算法:
var postOrder = function (node) {
if (node) {
postOrder(node.left);
postOrder(node.right);
console.log(node.value);
}
}
网友评论