Binary Tree Postorder Traversal LeetCode solution in JavaScript
1class Pair {2constructor(node, state) {3this.node = node;4this.state = state;5}6}7var postorderTraversal = function (root) {8let ans = [];9let st = []; // stack10root != null && st.push(new Pair(root, 1));1112while (st.length > 0) {13let top = st[st.length - 1];1415if (top.state == 1) {16top.state++;17if (top.node.left != null) {18st.push(new Pair(top.node.left, 1));19}20} else if (top.state == 2) {21top.state++;22if (top.node.right != null) {23st.push(new Pair(top.node.right, 1));24}25} else {26ans.push(top.node.val);27st.pop();28}29}30return ans;31};