Skip to content

Commit 9b4fd2f

Browse files
committed
Add prompt and solution for LC 965. Univalued Binary Tree
1 parent 2db1ce6 commit 9b4fd2f

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

965. Univalued Binary Tree.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
965. Univalued Binary Tree
4+
5+
A binary tree is uni-valued if every node in the tree has the same value.
6+
7+
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
8+
9+
10+
Example 1:
11+
Input: root = [1,1,1,1,1,null,1]
12+
Output: true
13+
14+
Example 2:
15+
Input: root = [2,2,2,5,2]
16+
Output: false
17+
18+
19+
Constraints:
20+
The number of nodes in the tree is in the range [1, 100].
21+
0 <= Node.val < 100
22+
23+
*/
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* function TreeNode(val, left, right) {
28+
* this.val = (val===undefined ? 0 : val)
29+
* this.left = (left===undefined ? null : left)
30+
* this.right = (right===undefined ? null : right)
31+
* }
32+
*/
33+
34+
/**
35+
* @param {TreeNode} root
36+
* @return {boolean}
37+
*/
38+
const isUnivalTreeDepth = (root) => {
39+
const dfs = node => {
40+
if (!node) return true;
41+
return node.val === root.val && dfs(node.left) && dfs(node.right);
42+
}
43+
44+
return dfs(root);
45+
};
46+
47+
/**
48+
* @param {TreeNode} root
49+
* @return {boolean}
50+
*/
51+
const isUnivalTreeBreadth = (root) => {
52+
const queue = [root];
53+
54+
while (queue.length) {
55+
const curr = queue.pop();
56+
if (curr.val !== root.val) return false;
57+
if (curr.left) queue.unshift(curr.left);
58+
if (curr.right) queue.unshift(curr.right);
59+
}
60+
61+
return true;
62+
};

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ LeetCode Profile: https://leetcode.com/timothyshores/
5151
|[844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|Easy|[JavaScript](844.%20Backspace%20String%20Compare.js.js)
5252
|[836. Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)|Easy|[JavaScript](836.%20Rectangle%20Overlap.js)
5353
|[896. Monotonic Array](https://leetcode.com/problems/monotonic-array/)|Easy|[JavaScript](896.%20Monotonic%20Array.js)
54-
|[938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|Medium|[JavaScript](938.%20Range%20Sum%20of%20BST.js)
54+
|[938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|Medium|[JavaScript](938.%20Range%20Sum%20of%20BST.js)
55+
|[965. Univalued Binary Tree.js](https://leetcode.com/problems/univalued-binary-tree/)|Easy|[JavaScript](965.%20Univalued%20Binary%20Tree.js)
5556
|[1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/)|Easy|[JavaScript](1047.%20Remove%20All%20Adjacent%20Duplicates%20In%20String.js
5657
|[1108. Defanging An IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|Easy|[JavaScript](1108.%20Defanging%20An%20IP%20Address.js)
5758
|[1170. Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|Medium|[JavaScript](1170.%20Compare%20Strings%20by%20Frequency%20of%20the%20Smallest%20Character.js)

0 commit comments

Comments
 (0)