Skip to content

Commit

Permalink
problem: new problem solution - 543 . Diameter of Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Nov 23, 2023
1 parent 579243a commit a4046e1
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | Algorithms | [TypeScript](./problems/algorithms/invertBinaryTree/InvertBinaryTree.ts) | Easy |
| 257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | Algorithms | [TypeScript](./problems/algorithms/binaryTreePaths/BinaryTreePaths.ts) | Easy |
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | Algorithms | [TypeScript](./problems/algorithms/sumOfLeftLeaves/SumOfLeftLeaves.ts) | Easy |
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | Algorithms | [TypeScript](./problems/algorithms/diameterOfBinaryTree/DiameterOfBinaryTree.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Source : https://leetcode.com/problems/diameter-of-binary-tree/
// Author : Francisco Tomas
// Date : 2023-11-23

import { NodeTree, diameterOfBinaryTree } from "./DiameterOfBinaryTree";

describe("diameter of binary tree", () => {
test("handle binary tree with two nodes", () => {
const result = diameterOfBinaryTree(
new NodeTree(1, new NodeTree(2, null, null), null),
);
expect(result).toBe(1);
});

test("handle slighty larger binary tree", () => {
const result = diameterOfBinaryTree(
new NodeTree(
1,
new NodeTree(
2,
new NodeTree(4, null, null),
new NodeTree(5, null, null),
),
new NodeTree(3, null, null),
),
);
expect(result).toBe(3);
});

test("handle large binary tree", () => {
const result = diameterOfBinaryTree(
new NodeTree(
4,
new NodeTree(-7, null, null),
new NodeTree(
-3,
new NodeTree(
-9,
new NodeTree(
9,
new NodeTree(
6,
new NodeTree(0, null, new NodeTree(-1, null, null)),
new NodeTree(6, new NodeTree(-4, null, null), null),
),
null,
),
new NodeTree(
-7,
new NodeTree(-6, new NodeTree(5, null, null), null),
new NodeTree(
-6,
new NodeTree(9, new NodeTree(-2, null, null), null),
null,
),
),
),
new NodeTree(-3, new NodeTree(-4, null, null), null),
),
),
);
expect(result).toBe(8);
});
});
124 changes: 124 additions & 0 deletions problems/algorithms/diameterOfBinaryTree/DiameterOfBinaryTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Source : https://leetcode.com/problems/diameter-of-binary-tree/
// Author : Francisco Tomas
// Date : 2023-11-23

/*****************************************************************************************************
*
* Given the root of a binary tree, return the length of the diameter of the tree.
*
* The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
* This path may or may not pass through the root.
*
* The length of a path between two nodes is represented by the number of edges between them.
*
* Example 1:
*
* Input: root = [1,2,3,4,5]
* Output: 3
* Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
*
* Example 2:
*
* Input: root = [1,2]
* Output: 1
*
* Constraints:
*
* The number of nodes in the tree is in the range [1, 10^4].
* -100 <= Node.val <= 100
******************************************************************************************************/

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 length of the diameter of the tree
* NOTES: The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
* This path may or may not pass through the root.
* The length of a path between two nodes is represented by the number of edges between them
* Stub:
function diameterOfBinaryTree(root: BT): number {return 0}
* Tests:
* I: root = [1,2,3,4,5] -> O: 3
* Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
* I: root = [1,2] -> O: 1
* I: root = [4,-7,-3,null,null,-9,-3,9,-7,-4,null,6,null,-6,-6,null,null,0,6,5,null,9,null,null,-1,-4,null,null,null,-2] -> O: 8
* Template:
function diameterOfBinaryTree(root: BT): number {
if (root === null) {return (...)}
else {
return (... root.val
(diameterOfBinaryTree(root.left))
(diameterOfBinaryTree(root.right)))
}
}
* Constraints:
* - The number of nodes in the tree is in the range [1, 104].
* - -100 <= Node.val <= 100
*/

/**
* Time Complexity: O(n^2), where n is the number of nodes in the given binary tree, root. This is because for each node of the tree height is called, which itself takes O(n) time.
* Space Complexity: O(h)
* Runtime: 85ms (12.17%)
* Memory: 47.63MB (11.42%)
*/
export function diameterOfBinaryTreeV1(root: BT): number {
if (root === null) {
return 0;
}
return Math.max(
height(root.left) + height(root.right),
diameterOfBinaryTree(root.left),
diameterOfBinaryTree(root.right),
);
}

/**
* Time Complexity: O(n)
* Space Complexity: O(h), where h is the height of the given binary tree, node.
*/
function height(node: BT): number {
if (node === null) {
return 0;
}
return 1 + Math.max(height(node.left), height(node.right));
}

/**
* Time Complexity: O(n)
* Space Complexity: O(h)
* Runtime: 50ms (98.50%)
* Memory: 46.41MB (89.51%)
*/
export function diameterOfBinaryTree(root: BT): number {
let ans: number = 0;
function height(node: BT): number {
if (node === null) {
return 0;
}
const left = height(node.left);
const right = height(node.right);
ans = Math.max(left + right, ans);
return 1 + Math.max(left, right);
}
height(root);
return ans;
}

0 comments on commit a4046e1

Please sign in to comment.