6,839 questions
Advice
1
vote
9
replies
112
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
votes
1
answer
109
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 ...
10
votes
3
answers
614
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)...
-1
votes
1
answer
86
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. ...
-1
votes
1
answer
66
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
1
answer
54
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:
...
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 ...
-4
votes
2
answers
230
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
2
answers
106
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
56
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
votes
1
answer
41
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, ...
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
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 ...
0
votes
1
answer
48
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 ...
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 ...
0
votes
1
answer
32
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.
...
-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 ...
3
votes
1
answer
119
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 ...
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 ...
2
votes
2
answers
114
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
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 ...
0
votes
1
answer
183
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 ...
0
votes
1
answer
113
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 ...
1
vote
2
answers
187
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 ...
1
vote
1
answer
83
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 ...