I was revising the topic of binary search trees and trying some questions.
This particular question is giving me wrong answer and I am not able to see any errors.
Can someone pls tell the error and also guide me what techniques should i follow to debug such codes own my own.
Problem Link:-
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool isLeaf(TreeNode *A)
{
return (!A->left&&!A->right);
}
void InorderTraversal(TreeNode* A,vector<int> & Nodes)
{
if(isLeaf(A))
{
Nodes.push_back(A->val);
return;
}
InorderTraversal(A->left,Nodes);
Nodes.push_back(A->val);
InorderTraversal(A->right,Nodes);
}
int Solution::kthsmallest(TreeNode* A, int B) {
vector<int> Nodes;
InorderTraversal(A,Nodes);
return Nodes[B-1];
}