Traversal of binary tree

Traversal of binary tree

Recursive Traversal of binary tree

function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

const preorderTraversal = function (root, res) {
  if (root === null) return;
  res.push(root.val);
  preorderTraversal(root.left, res);
  preorderTraversal(root.right, res);
};

const inorderTraversal = function (root, res) {
  if (root === null) return;
  inorderTraversal(root.left, res);
  res.push(root.val);
  inorderTraversal(root.right, res);
};

const postorderTraversal = function (root, res) {
  if (root === null) return;
  postorderTraversal(root.left, res);
  postorderTraversal(root.right, res);
  res.push(root.val);
};

var traversal = function (root) {
  let res = [];
  postorderTraversal(root, res);
  return res;
};

const tree1 = new TreeNode(1, null, new TreeNode(2, new TreeNode(3)));
const tree2 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree3 = new TreeNode(
  1,
  new TreeNode(
    2,
    new TreeNode(3, new TreeNode(4), new TreeNode(5)),
    new TreeNode(6)
  )
);

console.log(traversal(tree1));
console.log(traversal(tree2));
console.log(traversal(tree3));

Iterative Traversal of binary tree

function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

// mid - left - right(FILO)
const preorderTraversal = function (root, res, stack) {
  if (root === null) return;
  stack.push(root);
  while (stack.length) {
    let node = stack.pop();
    res.push(node.val);
    if (node.right) {
      stack.push(node.right);
    }
    if (node.left) {
      stack.push(node.left);
    }
  }
  return res;
};

const inorderTraversal = function (root, res, stack) {
  if (root === null) return;
  let node = root;
  while (node != null || stack.length) {
    if (node != null) {
      stack.push(node);
      node = node.left;
    } else {
      node = stack.pop();
      res.push(node.val);
      node = node.right;
    }
  }
  return res;
};

const postorderTraversal = function (root, res, stack) {
  if (root === null) return;
  stack.push(root);
  while (stack.length) {
    let node = stack.pop();
    res.push(node.val);
    if (node.left) {
      stack.push(node.left);
    }
    if (node.right) {
      stack.push(node.right);
    }
  }
  return res.reverse();
};

var traversal = function (root) {
  let res = [];
  let stack = [];
  postorderTraversal(root, res, stack);
  return res;
};

const tree1 = new TreeNode(1, null, new TreeNode(2, new TreeNode(3)));
const tree2 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree3 = new TreeNode(
  1,
  new TreeNode(
    2,
    new TreeNode(3, new TreeNode(4), new TreeNode(5)),
    new TreeNode(6)
  )
);

console.log(traversal(tree1));
console.log(traversal(tree2));
console.log(traversal(tree3));

Universal Iterative Traversal of binary tree

将访问的节点直接加入到栈中,但如果是处理的节点则后面放入一个空节点, 这样只有空节点弹出的时候,才将下一个节点放进结果集。

// 前序遍历:中左右
// 压栈顺序:右左中

var preorderTraversal = function (root, res = []) {
  const stack = [];
  if (root) stack.push(root);
  while (stack.length) {
    const node = stack.pop();
    if (!node) {
      res.push(stack.pop().val);
      continue;
    }
    if (node.right) stack.push(node.right); // 右
    if (node.left) stack.push(node.left); // 左
    stack.push(node); // 中
    stack.push(null);
  }
  return res;
};

//  中序遍历:左中右
//  压栈顺序:右中左

var inorderTraversal = function (root, res = []) {
  const stack = [];
  if (root) stack.push(root);
  while (stack.length) {
    const node = stack.pop();
    if (!node) {
      res.push(stack.pop().val);
      continue;
    }
    if (node.right) stack.push(node.right); // 右
    stack.push(node); // 中
    stack.push(null);
    if (node.left) stack.push(node.left); // 左
  }
  return res;
};

// 后续遍历:左右中
// 压栈顺序:中右左

var postorderTraversal = function (root, res = []) {
  const stack = [];
  if (root) stack.push(root);
  while (stack.length) {
    const node = stack.pop();
    if (!node) {
      res.push(stack.pop().val);
      continue;
    }
    stack.push(node); // 中
    stack.push(null);
    if (node.right) stack.push(node.right); // 右
    if (node.left) stack.push(node.left); // 左
  }
  return res;
};

Binary Tree Level Order Traversal

function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

const levelOrderTraversal = function (root, res, queue) {
  if (root === null) return [];
  queue.push(root);
  while (queue.length !== 0) {
    const length = queue.length; // the length of root array: 7
    let level = []; // store each level array
    for (let i = 0; i < length; i++) {
      let node = queue.shift(); // take the first num, i.e. the root node
      level.push(node.val);
      if (node.left) {
        queue.push(node.left);
      }
      if (node.right) {
        queue.push(node.right);
      }
    }
    res.push(level);
  }
  return res;
};

var traversal = function (root) {
  let res = [],
    queue = [];
  levelOrderTraversal(root, res, queue);
  return res;
};

const tree1 = new TreeNode(
  1,
  new TreeNode(
    2,
    new TreeNode(1, new TreeNode(2), new TreeNode(3)),
    new TreeNode(
      1,
      new TreeNode(1, new TreeNode(2), new TreeNode(3)),
      new TreeNode(1, new TreeNode(2), new TreeNode(3)),
      new TreeNode(1, new TreeNode(2), new TreeNode(3)),
      new TreeNode(1, new TreeNode(2), new TreeNode(3))
    )
  ),
  new TreeNode(2, new TreeNode(3))
);

console.log(traversal(tree1));
// [ [ 1 ], [ 2, 2 ], [ 1, 1, 3 ], [ 2, 3, 1, 1 ], [ 2, 3, 2, 3 ] ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值