6,838 questions
Advice
1
vote
9
replies
114
views
Sum of a binary tree
I have a binary tree which leafs have a size of 1, and the parent double in size at each level.
I project those nodes on a linear axe (memory space).
I need to get the equation that gives the adress ...
1
vote
2
answers
153
views
Given the string input, how can we traverse the binary tree recursively
Let's say our binary tree (bt) looks like this:
Node* root = new Node();
root->left = new Node("B");
root->right = new Node();
root->right->right = new Node("A");...
-1
votes
1
answer
112
views
How to check binary tree symmetry iteratively to avoid stack overflow with deep trees in Python [closed]
I have a working recursive solution to check if a binary tree is symmetric. The code works correctly for my test cases, but I'm concerned about potential stack overflow with deep trees since Python ...
1
vote
1
answer
158
views
LeetCode 199. Binary Tree Right Side View constant memory complexity
I'm trying to solve LeetCode 199.
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
I've ...
10
votes
3
answers
616
views
Better way to search for a node in binary tree
This is the code I have written for searching a node in a tree as a part of my assignment:
typedef struct NODE
{
int key;
struct NODE *left, *right;
} Node;
Node *search(Node *head, int key)...
0
votes
0
answers
44
views
How to determine left and right indices quickly in depth-first ordered implicit binary tree?
Suppose I have a complete, perfect binary tree stored in an array. If I store node data in breadth-first traversal order (Eytzinger order), then if an internal node's index is i, its left and right ...
-1
votes
1
answer
91
views
Why does modifying the returned pair from a recursive call break my solution in Binary Tree Consecutive Sequence II?
I'm trying to solve the Binary Tree Longest Consecutive Sequence II problem (Leetcode 549). The goal is to find the length of the longest consecutive path (increasing or decreasing) in a binary tree. ...
2
votes
1
answer
300
views
Is it possible to rebuild binary tree from its postorder and preorder traversal?
I understand we can rebuild a binary tree from its inorder AND ( postorder OR preorder ) traversal.
I however wonder whether rebuilding it from its postorder AND preorder traversal doable too?
-1
votes
1
answer
68
views
Not able to submit a binary tree code on gfg (vertical order traversal)
I was doing the GeeksforGeeks practice problem Vertical Tree Traversal:
Given a root of a Binary Tree, find the vertical traversal of it starting from the leftmost level to the rightmost level.
If ...
2
votes
0
answers
68
views
How to Implement rotateLeft rotateRight in tree based on array?
I'm trying to implement a binary tree using an array. It means that the following tree
1
/ \
2 3
/ \ / \
4 5 6 7
would be as the array below:
[1, 2, 3, 4, 5, 6, 7]
So it means that the ...
-2
votes
1
answer
60
views
Using list to find path sum of binary tree
def path_sum(root,temp):
print("root is",root)
if root == None:
return False
temp.append(root.val)
if root.left == None and root.right == None:
...
1
vote
0
answers
93
views
Implementing Correct Node Partnership in a Doubly-Ended Array-Based Priority Queue (DEAP)
I'm working on implementing a Double-Ended Priority Queue (DEAP) data structure in C++. I'm having trouble with establishing the correct implementation of node partnerships between the min and max ...
6
votes
1
answer
89
views
Is there a simple recursive analogue of this breadth-first binary tree deserialization function in Haskell?
I am interested in methods for serializing and deserializing a binary tree in breadth-first order. The first part, serialization, is equivalent to performing a levelorder traversal where we preserve ...
1
vote
1
answer
85
views
Print Binary Tree
I am trying to visually print out a simple binary tree in Java.
Here is the full code:
class WordPoint {
private final String word;
private final float pointValue;
public ...
-4
votes
2
answers
232
views
Level-Order Traversal of a Binary Tree Without Recursion? [closed]
I’m trying to implement a level-order traversal of a binary tree in C++ without using recursion. The goal is to traverse the tree level by level, starting from the root, and print the nodes in the ...
2
votes
1
answer
219
views
Data structure for easily finding the maximum length of a non-decreasing sequence on a certain interval
I'm trying to find a solution for this challenge:
Given is n, which is the size of an array filled with ones. There are two types of operations possible on this array:
Increase (x, y, m): add m to ...
2
votes
2
answers
110
views
Leetcode 1372: Why do these two code snippets give different results?
I am solving LeetCode problem 1372. Longest ZigZag Path in a Binary Tree:
You are given the root of a binary tree.
A ZigZag path for a binary tree is defined as follow:
Choose any node in the binary ...
0
votes
1
answer
130
views
gcc complains for infinite recursion while freeing binary tree
I am trying to write a generic free function for a binary tree.
typedef struct s_tree
{
void *content;
struct s_tree *left;
struct s_tree *right;
} t_tree;
void ft_treeclear(...
0
votes
1
answer
58
views
Quick method to merge Huffman Coding tree with repetitive entries
Say I have many repetitive entries that are to be merged into a Huffman Coding tree. Simply merging them would cost n*logn but I want it to be faster. Say I have 100000 entries of the same frequency 1,...
1
vote
1
answer
175
views
Using free() with data structures
I am not from a computer science background and have been trying to learn Data Structures using C programming.
I have made some binary tree programs and realised that I have used malloc() left and ...
-1
votes
1
answer
48
views
Recursive solution to compare the leaves of two binary trees returns wrong result
I am trying to solve LeetCode problem 872. Leaf Similar Trees:
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
For example, ...
0
votes
1
answer
92
views
Is this a top down or bottom up recursion
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class ...
1
vote
0
answers
146
views
Can we check whether one *alleged* directed binary tree contains a cycle in O(1) space?
This is from sicp although a bit different:
Exercise 3.18. Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of ...
0
votes
1
answer
134
views
How to render a custom node for @nivo/tree
I am trying to make a tree structure in react. I was trying with @nivo/tree. There's very limited documentation for this library. I couldn't get it to render a custom node. Attaching the code here.
...
0
votes
1
answer
112
views
Octree implementation in Rust: why is the insert function duplicating insertions and how could I go about fixing this?
I have been attempting to implement a voxel octree for a game, and this issue has completely stumped me.
My octree is stored linearly as a hashmap of octree indices and nodes (see "Implicit node ...
-2
votes
1
answer
89
views
Left View Method error in Binary tree using Python
I am learning DSA quite long.. During my learning, I encountered a concept 'Left View Method in Binary Tree'. I am completely scripted my code but there is a error when i tried to give a modified ...
0
votes
1
answer
49
views
Understanding the Leaves of Equal Height in a Huffman Tree for an Optimal Set-Based Search Algorithm
I'm studying an algorithm that uses a Huffman tree to find the optimal search cost based on probabilities. Here’s how the algorithm works:
Start by checking the most probable element (X1) to see if ...
8
votes
1
answer
476
views
Is the recursive approach to binary tree isomorphism actually linear?
I've seen contradicting claims about the worst case time complexity for a solution to the GeeksforGeeks challenge Check if Tree is Isomorphic:
Given two Binary Trees. Check whether they are ...
0
votes
0
answers
44
views
Why does #VALUE error in VBA function occur when creating a ReDim Matrix of Stock prices from a Named Range?
'This VBA binomial option code contains missing syntax intended to create an N by N matrix of stock prices using inputs for Pricing Binomial Tree Options with discrete dividends saved as a Named Range ...
3
votes
1
answer
148
views
Convert a sorted array into a height-balanced binary search tree by picking middle element -- why does it work?
I was doing an exercise to convert a sorted array to a binary search tree where every node in the tree has its children whose heights at most differ by 1.
One simple solution is to pick out the middle ...
0
votes
1
answer
39
views
Red Black Tree | is this tree balanced?
I have recently begun studying the structure of the red-black tree and am struggling to determine whether it is balanced. And explain why it is still balanced, or vice versa.
...
0
votes
2
answers
87
views
Difference between passing in string vs. array parameters to recursive function
This may be a dumb question so I am sorry, but there is a Leetcode problem where you have to return all of the paths from the root node to leaf nodes in an array of strings connected with "->&...
-1
votes
1
answer
35
views
There's an error in the function designed to check if a tree is a binary tree (not specifically a binary search tree)
I am working on a university project involving binary trees represented as dictionaries. I've implemented functions to check if these trees are full, complete, and binary, but my binary tree ...
-1
votes
1
answer
85
views
how to delete a binary tree throught another name
I have the following code:
typedef struct TreeNode
{
int data;
TreeNode *lchild, *rchild;
} TreeNode;
TreeNode *CreateBTNode(TreeNode *bt) // pre-build
{
char ch;
cin >> ch;
...
3
votes
1
answer
126
views
Correctness of Deletion algorithm of BST in CLRS
It's not quite obvious to me why the algorithm (see below) to delete a node from a binary search tree, that CLRS offers, works correctly (like how do we know that the inorder arrangement of the nodes ...
1
vote
0
answers
105
views
Heap vs Red-Black Tree
Why would we ever use a heap over a red-black tree?
Take a scenario where we want a min-heap. The time complexities of a mean-heap are as follows.
Peek: O(1) average/worst
Delete First: O(log(n)) ...
0
votes
1
answer
52
views
" heap-use-after-free" error even when I am not accessing the deleted node of a binary tree
I was trying to generate the inorder traversal of a binary tree using O(1) space ie I don't wanna use recursion or any other data structures such as queues or vectors to store the node.
Edit: This ...
0
votes
0
answers
41
views
What should be the comparison key when I have a coordinate pair binary tree
I want to create a binary tree out of 2d points. What should be the key to be compared for the binary tree?
2
votes
2
answers
118
views
Diameter of a binary tree - failing case when the longest path doesn't pass through the root
So I'm working on this problem:
Given the root of a binary tree, return the length of the diameter of the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in ...
0
votes
1
answer
63
views
There is an error in the non-recursive implementation of the preorder traversal of a binary tree, but there is no problem in debugging
When implementing a preorder traversal of a binary tree using a non-recursive approach, I wrote my own stack. An error occurred during runtime.
When I try to debug and find the problem, it runs ...
0
votes
2
answers
80
views
Recursive insert method on Binary Tree reaches the correct node but it doesn't save data
I'm trying to make a recursive insert method on a binary tree and while the recursive part of the function seems to do the job correctly (I tracked the code step by step with debug option) when time ...
-1
votes
1
answer
86
views
Binary Search Tree Insertion Method in Java [closed]
In the following insert method for a binary search tree (BST), the left side of the tree is being updated correctly, but there is an issue with inserting values on the right side. Despite using the ...
0
votes
1
answer
185
views
Is it possible to find distance between 2 nodes of a generic tree without lowest common ancestor (or path from root)
In case of Binary Trees, It is possible to find the distance between the two nodes without finding the path from the root node or lowest common ancestor.
I was wondering if it is possible in the case ...
2
votes
1
answer
124
views
Use of ampersand (&) along with arrow operator (->) for inputting data into a struct in C, is it necessary?
My professor is currently teaching Binary Trees at college and she gave us a program to create a binary tree in C using Linked Lists.
However, there was a debate on whether ampersand (&) has to be ...
0
votes
1
answer
120
views
Why am I getting "time limit exceeded" error in binary tree level order traversal problem in leetcode?
I'm trying to solve the problem: Binary tree level order traversal problem on LeetCode and I've also tried looking for the answers, but I'm still getting a time limit exceeded (TLE) error. Please help ...
0
votes
4
answers
134
views
Why using else if instead of if here when building a binary tree?
When building the insertion function in binary tree, I use two if statement to track the node( if the value is smaller than the current node,go left;if bigger,go right) .
I know the difference between ...
1
vote
2
answers
189
views
Construct binary tree from preorder and inorder traversal: wrong tree
I am looking at LeetCode problem 105. Construct Binary Tree from Preorder and Inorder Traversal:
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary ...
0
votes
1
answer
213
views
Red Black Tree Black Height Increase after Insertion
I understand that the black height of a RB tree increases after insertion when a red-red violation is propagated to the root, which then gets colored to red then recolored to black, and it causes for ...
1
vote
1
answer
89
views
How to represent an algebraic expressions tree in a non-ambiguous way?
I have some physical model that needs to be represented as an algebraic expressions full binary tree. How can I represent such a tree in a non-ambiguous way?
Consider, as an example, the three full ...
3
votes
1
answer
239
views
Optimally counting number of nodes in a complete binary tree
I am looking at LeetCode problem 222. Count Complete Tree Nodes:
Given the root of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except ...