I was solving this question. When the min and max variables are set as global variable, I’m getting the correct output, but when I pass them in the functions it messes up the output. I can’t figure out the reason. Can someone tell me, how are these two code snippets different. global: int min = ..

#### Category : binary-tree

I have to return the elements of a binary tree in an int vector. I have written the following code, but it gives segmentation fault. What could be the reason for that? Also, does my code use correct logic? struct Node { int data; struct *Node left; struct *Node right; } Node(int x) { data ..

So I want to implement a nested binary tree of depth 2 for a project in which every node of the primary tree is actually the root node of a secondary tree. And I want to create a tree that can later be used by my friends to add more nodes. So I know the ..

This is a leetcode problem "Construct Binary Tree from Preorder and Inorder Traversal" and in this solution I am wondering why do we have pass "int rootIdx = 0;" as an address, and why my code fails if I just pass it by value. class Solution { public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { int ..

I have implemented a simple binary tree class in C++. have using smart pointers objects to hold the pointers to each node (shared for children and weak for parent). I was trying to implement a nested class for custom iterator (in-order, pre-order and post-order), but I couldn’t figure out how to implement efficiently the .next() ..

In the below code root contains the root of our tree and sum contains our result. This code seems fine to me but when i submitted it on the GFG :- Largest Bst in a binary tree it is giving error please help. int po(Node *root,int &sum){ if(root==NULL) return 0; int p=po(root->left,sum); int q=po(root->right,sum); int ..

I’m trying to figure out a function in c++ to calculate the sums of all the levels of a binary tree and then checking if those sums are all equal, and returning TRUE if they are. So for example, if the first node is 10, the sum of its children has to be 10, and ..

I am trying to remove the subtree given a node, but it is not working properly. Here’s my code: void remove(T node) { int i; for (i = 0; i < maxNodes; i++) { if (data[i] == node) break; } if (data[i] != node) { cout << "Node does not exist."; return; } bool flag ..

I am having some trouble with a pointer. I have been working on the exact same idea for almost a week, keep doing the same thing, same error, trying with different ideas but still I am not able to make it work. Someone has answered my questions saying that creating a pointer in a function ..

I have coded Binary Tree traversals and its height in C++ but when compiling after a bit of coding but I keep getting the error: Return Statement with no value, in function returning ‘int’ (-fpermissive). Here is my code: #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *right; struct node *left; ..

## Recent Comments