#### Category : binary-tree

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 = ..

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 ..