Skip to content

Instantly share code, notes, and snippets.

@ykdojo
Created March 25, 2019 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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