Skip to content

Commit

Permalink
problem: new problem solution - 101 . Symmetric Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Nov 6, 2023
1 parent dff0e4b commit 7458a10
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | Algorithms | [TypeScript](./problems/algorithms/insertIntoABinarySearchTree/InsertIntoABinarySearchTree.ts) | Medium |
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | Algorithms | [TypeScript](./problems/algorithms/binaryTreeInorderTraversal/BinaryTreeInorderTraversal.ts) | Easy |
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | Algorithms | [TypeScript](./problems/algorithms/sameTree/SameTree.ts) | Easy |
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | Algorithms | [TypeScript](./problems/algorithms/symmetricTree/SymmetricTree.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
55 changes: 55 additions & 0 deletions problems/algorithms/symmetricTree/SymmetricTree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Source : https://leetcode.com/problems/symmetric-tree/
// Author : squxq
// Date : 2023-11-06

import { NodeTree, isSymmetric, type BT } from "./SymmetricTree"; // Import the necessary code

describe("isSymmetric function", () => {
it("returns true for an empty tree", () => {
const root: BT = null;
expect(isSymmetric(root)).toBe(true);
});

it("returns true for a single-node tree", () => {
const root: BT = new NodeTree(1);
expect(isSymmetric(root)).toBe(true);
});

it("returns true for a symmetric two-node tree", () => {
const root: BT = new NodeTree(1, new NodeTree(2), new NodeTree(2));
expect(isSymmetric(root)).toBe(true);
});

it("returns false for a non-symmetric two-node tree", () => {
const root: BT = new NodeTree(1, new NodeTree(2), new NodeTree(3));
expect(isSymmetric(root)).toBe(false);
});

it("returns true for a symmetric three-node tree", () => {
const root: BT = new NodeTree(1, new NodeTree(2), new NodeTree(2));
expect(isSymmetric(root)).toBe(true);
});

it("returns false for a non-symmetric three-node tree", () => {
const root: BT = new NodeTree(1, new NodeTree(2), new NodeTree(3));
expect(isSymmetric(root)).toBe(false);
});

it("returns true for a complex symmetric tree", () => {
const root: BT = new NodeTree(
1,
new NodeTree(2, new NodeTree(3), new NodeTree(4)),
new NodeTree(2, new NodeTree(4), new NodeTree(3)),
);
expect(isSymmetric(root)).toBe(true);
});

it("returns false for a complex non-symmetric tree", () => {
const root: BT = new NodeTree(
1,
new NodeTree(2, new NodeTree(3), new NodeTree(4)),
new NodeTree(2, new NodeTree(4), new NodeTree(5)),
);
expect(isSymmetric(root)).toBe(false);
});
});
101 changes: 101 additions & 0 deletions problems/algorithms/symmetricTree/SymmetricTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Source : https://leetcode.com/problems/symmetric-tree/
// Author : squxq
// Date : 2023-11-06

/*****************************************************************************************************
*
* Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its
* center).
*
* Example 1:
*
* Input: root = [1,2,2,3,4,4,3]
* Output: true
*
* Example 2:
*
* Input: root = [1,2,2,null,3,null,3]
* Output: false
*
* Constraints:
*
* The number of nodes in the tree is in the range [1, 1000].
* -100 <= Node.val <= 100
*
* Follow up: Could you solve it both recursively and iteratively?
******************************************************************************************************/

/**
* Binary Tree (BT) is one of:
* - null
* - NodeTree
* interp. null means no BT, or empty BT
* NodeTree is the BT's root
*/
export type BT = NodeTree | null;

/**
* NodeTree is new NodeTree(number, BT, BT)
* interp. new NodeTree(val, left, right) is a BT's node with:
* val, is the node's key
* left, is the node's left subtree
* right, is the node's right subtree
* INVARIANT:
* - each node has at most 2 children (left and right)
* Template:
function fn-for-BT(root: BT) {
if (root === null) {return (...)}
else {
return (... (root.val)
(fn-for-BT(root.left))
(fn-for-BT(root.right)))
}
}
*/
export class NodeTree {
val: number;
left: NodeTree | null;
right: NodeTree | null;
constructor(val?: number, left?: NodeTree | null, right?: NodeTree | null) {
this.val = val ?? 0;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}

/**
* BT -> boolean
* given the root of a binary tree, root, return true if it is a mirror of itself
* NOTE: it is symmetrci around its center
* Stub:
funtion isSymmetric(root: BT): boolean {return false}
* Tests:
* I: root = [1,2,2,3,4,4,3] -> O: true
* I: root = [1,2,2,null,3,null,3] -> O: false
* Template: <used template from BT>
* CONSTRAINTS:
* - The number of nodes in the tree is in the range [1, 1000].
* - -100 <= Node.val <= 100
*/
export function isSymmetric(root: BT): boolean {
return isMirror(root, root);
}

function isMirror(root1: BT, root2: BT): boolean {
if (root1 === null && root2 === null) {
return true;
}
if (root1 === null || root2 === null) {
return false;
}
return (
root1.val === root2.val &&
isMirror(root1.left, root2.right) &&
isMirror(root1.right, root2.left)
);
}

0 comments on commit 7458a10

Please sign in to comment.