Unival Tree, by Daily Coding Problem
Here was today's problem, asked (so they say) by Google:
Not sure you'll ever run into such a problem in real life, but in the rare case you do...
My approach was to do a post-order traversal of the tree. The function actually returns whether the current (sub)tree is a unival or not. It also carries a variable by reference which increments the number of unival subtrees seen so far.
The base case returns true for when the tree is null, and the unival count is set to zero.
After you call for left and right, the unival count is incremented right away with the values coming from the left and from the right.
Next comes the processing of the current node. All you have to do is check whether the current node is the root of a unival tree by precisely following the definition. If so, increment it and return "true", otherwise "false".
Code has been posted to GitHub: https://github.com/marcelodebarros/dailycodingproblem it is also down below. Hope you enjoy it, thanks, Marcelo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DailyCodingProblem
{
class DailyCodingProblem09222018
{
private readonly Tree tree = null;
public DailyCodingProblem09222018()
{
tree = BuildTree();
}
public int Unival()
{
int count = 0;
_UniVal(tree, ref count);
return count;
}
private bool _UniVal(Tree tree, ref int count)
{
//Base case
if (tree == null)
{
count = 0;
return true; //Axiom: null tree is unival
}
//Post-order L and then R
int leftCount = 0;
bool isLeftUnival = _UniVal(tree.left, ref leftCount);
int rightCount = 0;
bool isRightUnival = _UniVal(tree.right, ref rightCount);
//Process current
count += leftCount + rightCount;
if ((tree.left == null || (isLeftUnival && tree.left.val == tree.val)) &&
(tree.right == null || (isRightUnival && tree.right.val == tree.val)))
{
count++;
return true;
}
return false;
}
private Tree BuildTree()
{
Tree tree = new Tree(0)
{
left = new Tree(1),
right = new Tree(0)
{
left = new Tree(1)
{
left = new Tree(1),
right = new Tree(1)
},
right = new Tree(0)
}
};
return tree;
}
}
internal class Tree
{
public int val = 0;
public Tree left = null;
public Tree right = null;
public Tree(int val)
{
this.val = val;
this.left = null;
this.right = null;
}
}
}
Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0
/ \
1 0
/ \
1 0
/ \
1 1
Not sure you'll ever run into such a problem in real life, but in the rare case you do...
My approach was to do a post-order traversal of the tree. The function actually returns whether the current (sub)tree is a unival or not. It also carries a variable by reference which increments the number of unival subtrees seen so far.
The base case returns true for when the tree is null, and the unival count is set to zero.
After you call for left and right, the unival count is incremented right away with the values coming from the left and from the right.
Next comes the processing of the current node. All you have to do is check whether the current node is the root of a unival tree by precisely following the definition. If so, increment it and return "true", otherwise "false".
Code has been posted to GitHub: https://github.com/marcelodebarros/dailycodingproblem it is also down below. Hope you enjoy it, thanks, Marcelo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DailyCodingProblem
{
class DailyCodingProblem09222018
{
private readonly Tree tree = null;
public DailyCodingProblem09222018()
{
tree = BuildTree();
}
public int Unival()
{
int count = 0;
_UniVal(tree, ref count);
return count;
}
private bool _UniVal(Tree tree, ref int count)
{
//Base case
if (tree == null)
{
count = 0;
return true; //Axiom: null tree is unival
}
//Post-order L and then R
int leftCount = 0;
bool isLeftUnival = _UniVal(tree.left, ref leftCount);
int rightCount = 0;
bool isRightUnival = _UniVal(tree.right, ref rightCount);
//Process current
count += leftCount + rightCount;
if ((tree.left == null || (isLeftUnival && tree.left.val == tree.val)) &&
(tree.right == null || (isRightUnival && tree.right.val == tree.val)))
{
count++;
return true;
}
return false;
}
private Tree BuildTree()
{
Tree tree = new Tree(0)
{
left = new Tree(1),
right = new Tree(0)
{
left = new Tree(1)
{
left = new Tree(1),
right = new Tree(1)
},
right = new Tree(0)
}
};
return tree;
}
}
internal class Tree
{
public int val = 0;
public Tree left = null;
public Tree right = null;
public Tree(int val)
{
this.val = val;
this.left = null;
this.right = null;
}
}
}
Comments
Post a Comment