Code Monkey home page Code Monkey logo

Comments (2)

webVueBlog avatar webVueBlog commented on September 25, 2024

144. 二叉树的前序遍历

  • 前序遍历:中,左,右
  • 栈的特点是先进后出,因此出栈顺序为中,左,右
  • 那么入栈顺序必须调整为出栈顺序的倒序,也就是右,左,中
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */

// BFS 解法一
var preorderTraversal = function (root) {
  if (!root) return []

  const ans = []
  const stack = [root]

  while (stack.length) {
    const { val, left, right } = stack.pop()

    ans.push(val) // 处理当前节点
    if (right) stack.push(right) // 先右
    if (left) stack.push(left) // 后左
  }

  return ans
}

/* ------------------------------------------------ 分割线----------------------------------------------------- */

// BFS 解法二
var preorderTraversal = function (root) {
  if (!root) return []

  const ans = []
  const stack = [root]
  while (stack.length) {
    let curr = stack.pop()
    while (curr) {
      ans.push(curr.val)
      if (curr.right) stack.push(curr.right) // push右子节点
      curr = curr.left // 将指针指向左子节点
    }
  }

  return ans
}

// 了解到层序遍历的顺序之后,我们就可以通过循环利用指针的特性逐步推送到下一层
// 此方法在in-order和post-order的实现中较常用到

94. 二叉树的中序遍历

中序遍历:左,中,右
栈的特点是先进后出,因此出栈顺序为左,中,右
那么入栈顺序必须调整为出栈顺序的倒序,也就是右,中,左

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
  const stack = []
  const ans = []

  while (root || stack.length) {
    while (root) {
      stack.push(root)
      root = root.left
    }

    root = stack.pop()
    ans.push(root.val)
    root = root.right
  }

  return ans
}

145. 二叉树的后序遍历

  • 后序遍历:左,右,中
  • 栈的特点是先进后出,因此出栈顺序为左,右,中
  • 那么入栈顺序必须调整为出栈顺序的倒序,也就是中,右,左
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */

// BFS
var postorderTraversal = function (root) {
  if (!root) return []

  const stack = []
  const ans = []
  let curr = root

  while (stack.length || curr) {
    if (curr) {
      ans.push(curr.val)
      stack.push(curr) // 储存节点
      curr = curr.right // 每次先遍历右节点,再遍历左节点
    } else {
      const node = stack.pop() // 右子树走到底之后,再从获取已经遍历过右子树的中间结果,将它出栈,并遍历它的左子树
      curr = node.left
    }
  }

  return ans.reverse()
}

from mini-vue.

webVueBlog avatar webVueBlog commented on September 25, 2024

[144] 二叉树的前序遍历

var preorderTraversal = function (root) {
  if(!root) return []
  const ans = []
  const stack = [root]
  while(stack.length) {
    const {val,left,right} = stack.pop()
    ans.push(val)
    if(right) stack.push(right)
    if(left) stack.push(left)
  }
  return ans
}

// DFS 直接套用模板即可
var preorderTraversal = function (root) {
  return getTree(root)
}

const getTree = (node, arr = []) => {
  if (!node) return arr

  arr.push(node.val) // 处理当前节点
  getTree(node.left, arr)
  getTree(node.right, arr)

  return arr
}

from mini-vue.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.