Skip to content

Commit

Permalink
problem: new problem solution - 145 . Binary Tree Postorder Traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Nov 13, 2023
1 parent 86ebc8d commit bac8b17
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | Algorithms | [TypeScript](./problems/algorithms/minimumDepthOfBinaryTree/MinimumDepthOfBinaryTree.ts) | Easy |
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | Algorithms | [TypeScript](./problems/algorithms/pathSum/PathSum.ts) | Easy |
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | Algorithms | [TypeScript](./problems/algorithms/binaryTreePreorderTraversal/BinaryTreePreorderTraversal.ts) | Easy |
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | Algorithms | [TypeScript](./problems/algorithms/binaryTreePostorderTraversal/BinaryTreePostorderTraversal.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/
// Author : squxq
// Date : 2023-11-13

import { NodeTree, postorderTraversal } from "./BinaryTreePostorderTraversal"; // Import your code file

describe("postorderTraversal", () => {
it("should return an empty array for an empty tree", () => {
const root: NodeTree | null = null;
expect(postorderTraversal(root)).toEqual([]);
});

it("should return the correct postorder traversal for a single node tree", () => {
const root: NodeTree = new NodeTree(1);
expect(postorderTraversal(root)).toEqual([1]);
});

it("should return the correct postorder traversal for a tree with left children", () => {
const root: NodeTree = new NodeTree(1, new NodeTree(2), new NodeTree(3));
expect(postorderTraversal(root)).toEqual([2, 3, 1]);
});

it("should return the correct postorder traversal for a tree with right children", () => {
const root: NodeTree = new NodeTree(
1,
null,
new NodeTree(2, null, new NodeTree(3)),
);
expect(postorderTraversal(root)).toEqual([3, 2, 1]);
});

it("should handle negative values in the tree", () => {
const root: NodeTree = new NodeTree(-1, new NodeTree(-2), new NodeTree(-3));
expect(postorderTraversal(root)).toEqual([-2, -3, -1]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/
// Author : squxq
// Date : 2023-11-13

/*****************************************************************************************************
*
* Given the root of a binary tree, return the postorder traversal of its nodes' values.
*
* Example 1:
*
* Input: root = [1,null,2,3]
* Output: [3,2,1]
*
* Example 2:
*
* Input: root = []
* Output: []
*
* Example 3:
*
* Input: root = [1]
* Output: [1]
*
* Constraints:
*
* The number of the nodes in the tree is in the range [0, 100].
* -100 <= Node.val <= 100
*
* Follow up: Recursive solution is trivial, could you do it iteratively?
******************************************************************************************************/

export type BT = NodeTree | null;

export class NodeTree {
val: number;
left: BT;
right: BT;
constructor(val?: number, left?: BT, right?: BT) {
this.val = val ?? 0;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}

/**
* BT -> number[]
* given the root of a binary tree, root, return the postorder traversal of its node's values
* Stub:
function postorderTraversal(root: BT): number[] {return []}
* Tests:
* I: root = [1,null,2,3] -> O: [3,2,1]
* I: root = [] -> O: []
* I: root = [1] -> O: [1]
* Template:
function postorderTraversal(root: BT): number[] {
if (root === null) {return (...)}
else {
return (... (root.val)
(postorderTraversal(root.left))
(postorderTraversal(root.right)))
}
}
* CONSTRAINTS:
* - The number of the nodes in the tree is in the range [0, 100].
* - -100 <= Node.val <= 100
*/
export function postorderTraversal(root: BT): number[] {
if (root === null) {
return [];
}
return [
...postorderTraversal(root.left),
...postorderTraversal(root.right),
root.val,
];
}

0 comments on commit bac8b17

Please sign in to comment.