Given a binary tree, return the postorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]1\2/3
Output: [3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
function TreeNode(val) {this.val = valthis.left = this.right = null}var tree1 = new TreeNode(1)var tree2 = new TreeNode(2)var tree3 = new TreeNode(3)tree2.left = tree3tree1.left = nulltree1.right = tree2
/*** Definition for a binary tree node.* function TreeNode(val) {* this.val = val;* this.left = this.right = null;* }*//*** @param {TreeNode} root* @return {number[]}*/var postorderTraversal = function(root) {if (root) {return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val]} else {return []}}
使用颜色标记法
剖析树在中序遍历下的递归操作, 思路如下:
中 -> 右 -> 左
的顺序推入栈, 同时将白色元素标记为灰色元素;推荐使用颜色标记法, 它的解题思路适用于解前序、中序、后序遍历。
1/ \2 5/ \3 4
在如上所示树中, 模拟系统栈图解其执行过程如下:
gray 1white 2white 5white 2white 5gray 2white 3white 4white 5
/*** Definition for a binary tree node.* function TreeNode(val) {* this.val = val;* this.left = this.right = null;* }*//*** @param {TreeNode} root* @return {number[]}*/var postorderTraversal = function(root) {const printArr = []if (!root) return printArrconst stack = []stack.push({ color: 'white', node: root })while (stack.length > 0) {const { color, node } = stack.pop()if (color === 'gray') {printArr.push(node.val)} else {stack.push({ color: 'gray', node })node.right && stack.push({ color: 'white', node: node.right })node.left && stack.push({ color: 'white', node: node.left })}}return printArr}
94、144