Skip to content

Latest commit

 

History

History
29 lines (25 loc) · 486 Bytes

后序遍历.md

File metadata and controls

29 lines (25 loc) · 486 Bytes

后序遍历

来源

leetcode: 传送门

代码

function postOrderTraversal(root) {
  let res = [];
  let stack = [];
  if (root == null) {
    return res;
  }
  stack.push(root);
  while (stack.length) {
    let node = stack.pop();
    res.unshift(node.val);
    if (root.left) {
      stack.push(node.left);
    }
    if (root.right) {
      stack.push(node.right);
    }
  }
  return res;
}