-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: new problem solution - 145 . Binary Tree Postorder Traversal
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
problems/algorithms/binaryTreePostorderTraversal/BinaryTreePostorderTraversal.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
problems/algorithms/binaryTreePostorderTraversal/BinaryTreePostorderTraversal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} |