BT3 二叉树的后序遍历
约 185 字小于 1 分钟
2025-04-15
描述
给定一个二叉树,返回他的后序遍历的序列。
后序遍历是值按照 左节点->右节点->根节点 的顺序的遍历。
链接
示例
{1,#,2,3} => [3,2,1]
{} => []
{1} => [1]题解
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
function postorderTraversal(root) {
// write code here
function go(tree, arr) {
if (tree) {
go(tree.left, arr);
go(tree.right, arr);
arr.push(tree.val, arr);
}
}
let res = [];
go(root, res);
return res;
}
module.exports = {
postorderTraversal: postorderTraversal,
};