描述
给定一个二叉树,返回它的 后序 遍历。
示例 1:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
题解
递归法:
/**
* 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 postorderTraversal = function(root) {
let list = []
postorder(root, list)
return list
};
const postorder = (root, list) => {
if (!root) return null
postorder(root.left, list)
postorder(root.right, list)
list.push(root.val)
return list
}