Skip to content

Commit dca54f1

Browse files
authored
Create 965-univalued-binary-tree.js
1 parent 1ad5623 commit dca54f1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

965-univalued-binary-tree.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
const isUnivalTree = function(root) {
13+
const arr = []
14+
dfs(root, arr)
15+
for(let i = 1; i < arr.length; i++) {
16+
if(arr[i] !== arr[i- 1]) return false
17+
}
18+
return true
19+
};
20+
21+
function dfs(node, arr) {
22+
if(node === null) return
23+
arr.push(node.val)
24+
dfs(node.left, arr)
25+
dfs(node.right, arr)
26+
}

0 commit comments

Comments
 (0)