Open Zheaoli opened 1 year ago
/*
* @lc app=leetcode id=145 lang=typescript
*
* [145] Binary Tree Postorder Traversal
*/
// @lc code=start
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function postorderTraversal(root: TreeNode | null): number[] {
const ans = [];
const walk = (node) => {
node?.left && walk(node.left);
node?.right && walk(node.right);
node?.val && ans.push(node.val);
}
walk(root);
return ans;
};
// @lc code=end
微信id: 弘树 来自 vscode 插件
2022-10-21