0% found this document useful (0 votes)
26 views

Binary Tree

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Binary Tree

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1:

int height(Node* node)


{
int leftHeight = 0;
int rightHeight = 0;
if (node == nullptr)
{
return 0;
}
else
{
leftHeight = height(node->pLeft);
rightHeight = height(node->pRight);
}

if (leftHeight > rightHeight)


{
return leftHeight + 1;
}
else
{
return rightHeight + 1;
}

void printCurrentLevel(Node* root, int level)


{
if (root == nullptr)
{
return;
}
if (level == 1)
{
cout << root->value;
cout << " ";
}
else
{
printCurrentLevel(root->pLeft, level - 1);
printCurrentLevel(root->pRight, level - 1);
}
}

void BFS()
{
Node* node = root;
int h = height(node);
for (int i = 1; i <= h; i++)
{
printCurrentLevel(node, i);
}
}

Q2
void longestPathSum(BTNode* node, int sum, int len, int& maxLen, int& maxSum)
{
if (node == nullptr)
{
if (maxLen < len)
{
maxLen = len;
maxSum = sum;
}
else if (maxLen == len && maxSum < sum)
{
maxSum = sum;
}
return;
}
longestPathSum(node->left, sum + node->val, len + 1, maxLen, maxSum);
longestPathSum(node->right, sum + node->val, len + 1, maxLen, maxSum);
}

int longestPathSum(BTNode* root)


{
if (root == nullptr)
{
return 0;
}
int maxSum = 0, maxLen = 0;
longestPathSum(root, 0, 0, maxLen, maxSum);
return maxSum;
}

Q3
BTNode* lowestCommonAncestor(BTNode* root, int a, int b)
{
if (root == nullptr || root->val == a || root->val == b)
{
return root;
}

BTNode* left = lowestCommonAncestor(root->left, a, b);


BTNode* right = lowestCommonAncestor(root->right, a, b);

if (left != nullptr && right != nullptr)


{
return root;
}

if (left != nullptr)
{
return left;
}

return right;
}

int lowestAncestor(BTNode* root, int a, int b)


{
BTNode* lowestAncestorNode = lowestCommonAncestor(root, a, b);
return lowestAncestorNode->val;
}

Q4
int sumDigitPath(BTNode* node, int currentSum)
{
if (node == nullptr)
{
return 0;
}

currentSum = (currentSum * 10 + node->val) % 27022001;

if (node->left == nullptr && node->right == nullptr)


{
return currentSum;
}

int sum = 0;
if (node->left != nullptr)
{
sum = (sum + sumDigitPath(node->left, currentSum)) % 27022001;
}
if (node->right != nullptr)
{
sum = (sum + sumDigitPath(node->right, currentSum)) % 27022001;
}

return sum;
}

int sumDigitPath(BTNode* root)


{
return sumDigitPath(root, 0);
}

Q5
int countTwoChildrenNode(Node* node)
{
if (node == nullptr)
{
return 0;
}
int count = 0;
if (node->pLeft != nullptr && node->pRight != nullptr)
{
count = 1;
}
int leftCount = countTwoChildrenNode(node->pLeft);
int rightCount = countTwoChildrenNode(node->pRight);
return count + leftCount + rightCount;
}

int countTwoChildrenNode()
{
return countTwoChildrenNode(root);
}

Q6
int getHeight(Node* node)
{
if (node == nullptr)
{
return 0;
}
int leftHeight = getHeight(node->pLeft);
int rightHeight = getHeight(node->pRight);
if (leftHeight >= rightHeight)
{
return leftHeight + 1;
}
else
{
return rightHeight + 1;
}
}

void preOrder(Node* node)


{
if (node == nullptr) return;
cout << node->value << " ";
preOrder(node->pLeft);
preOrder(node->pRight);
}

void inOrder(Node* node)


{
if (node == nullptr) return;
inOrder(node->pLeft);
cout << node->value << " ";
inOrder(node->pRight);
}

void postOrder(Node* node)


{
if (node == nullptr) return;
postOrder(node->pLeft);
postOrder(node->pRight);
cout << node->value << " ";
}

int getHeight()
{
return getHeight(root);
}

string preOrder()
{
string s = "";
preOrder(root);
return s;
}

string inOrder()
{
string s = "";
inOrder(root);
return s;
}

string postOrder()
{
string s = "";
postOrder(root);
return s;
}

Q7
int sumOfLeafs(Node* node)
{
if (node == nullptr)
{
return 0;
}

if (node->pLeft == nullptr && node->pRight == nullptr)


{
return node->value;
}

int leftSum = sumOfLeafs(node->pLeft);


int rightSum = sumOfLeafs(node->pRight);

return leftSum + rightSum;


}

int sumOfLeafs()
{
return sumOfLeafs(root);
}

You might also like