Skip to content

Instantly share code, notes, and snippets.

@ykdojo
Created March 25, 2019 18:31
Show Gist options
  • Save ykdojo/7b6d637479dcace031bcea62e8ee8a79 to your computer and use it in GitHub Desktop.
Save ykdojo/7b6d637479dcace031bcea62e8ee8a79 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isUnivalTree = function(root) {
return helper(root, root.val);
};
var helper = function(root, val) {
if (!root) {
return true;
}
if (root.val != val) {
return false;
}
if (!helper(root.left, val) || !helper(root.right, val)) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment