Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
15 votes
Accepted

Max-heap implementation in C

This is a max-heap datastructure, and there is very consistent nomenclature here which reflects that. I'm sure it is clear and commendable. But I wouldn't have minded seeing it written in a comment, ...
J_H's user avatar
  • 42.7k
11 votes

Max-heap implementation in C

General Observations Since this might be a set of library routines, it might be a good idea to move all the heap related functions to their own .c file and have a ...
pacmaninbw's user avatar
  • 26.2k
8 votes
Accepted

Check whether a binary tree is symmetric

I note that this algorithm determines tests whether the tree's structure is symmetric, as well as the content. I haven't seen the problem specification, so have to assume that that is a requirement - ...
Toby Speight's user avatar
  • 88.4k
7 votes

Max-heap implementation in C

Avoid FP math for an integer problem floor(log2(max_heap->size)) is overkill and at some level risks incorrect results with less than ideal FP functions. A ...
chux's user avatar
  • 36.5k
7 votes

Max-heap implementation in C

The biggest issue I see here, and is not yet commented on, is the call to realloc for every insertion. When you remove an element from the heap, the ...
Cris Luengo's user avatar
  • 7,031
7 votes

Check whether a binary tree is symmetric

communicating with humans Something that can take a long time for junior devs to realize is that coding is primarily about communicating a technical idea to colleagues. programs must be written for ...
J_H's user avatar
  • 42.7k
6 votes
Accepted

A toy implementation of binary tree

Nested functions are not in the C standard. It is a GCC extension, of a dubious value. Don't do it. Casting malloc is a bad practice. First, it is not necessary, ...
vnp's user avatar
  • 58.7k
6 votes

Check whether a binary tree is symmetric

In addition to the previous answer by @Toby Speight, here are some general coding style suggestions. Documentation The PEP 8 style guide recommends adding docstrings for classes and functions. You ...
toolic's user avatar
  • 16.1k
5 votes
Accepted

Simple BST implementation using C++ Struct

Neither of your implementations is "advantageous" over other because both of them are C-style, unidiomatic C++ code — they are really C code with a handful of C++ additions. Moreover, they ...
L. F.'s user avatar
  • 9,715
5 votes
Accepted

Simple Binary Search Tree Class with Insert and Search Functions

Overall organization Right now, you've defined a BstNode, which you use directly in main. I'd at least consider defining a <...
Jerry Coffin's user avatar
  • 34.1k
5 votes
Accepted

Create and view a family tree

This is by no means exhaustive, more a list of general highlights on what to focus on rather that deep analysis of the implementation. Some general pieces of advice first: Use some coding conventions ...
alagner's user avatar
  • 166
5 votes
Accepted

Binary Expression Tree that evaluates Postfix input

Validate inputs It's not mentioned if we can assume the input is always valid. Aside from parsing errors, if the input is not in valid postfix form, the parsed tree may be broken, consider for example ...
janos's user avatar
  • 113k
5 votes

A toy implementation of binary tree

I would be a little more careful and consistent with my styling. You have if (curDepth == maxDepth) {return NULL;} but you later have ...
Ethan Slota's user avatar
5 votes

Binary tree deepcopy using the Morris traversal in C

single responsibility I need to perform a deep copy of a binary tree using the Morris traversal. Ok, maybe you. I confess it's not obvious to me why that is. Consider making the underlying binary ...
J_H's user avatar
  • 42.7k
4 votes

Simple Binary Search Tree Class with Insert and Search Functions

This program is using purely C++ I/O so the include for the old style C I/O is not necessary, you don't need the #include <stdio.h> and it would be better to ...
pacmaninbw's user avatar
  • 26.2k
4 votes
Accepted

Sort array of numbers using tree sort - Leetcode 912

edge case I feel the first result displayed here is reasonable: >>> sorted([]) [] >>> sorted([6, 4]) [4, 6] Given zero elements, and asked to ...
J_H's user avatar
  • 42.7k
4 votes

Binary tree deepcopy using the Morris traversal in C

tNode* node = (tNode *)malloc(sizeof(tNode)); In ancient times, malloc() used to return a ...
Madagascar's user avatar
  • 10.1k
3 votes
Accepted

How can I optimize this Binary Search Tree?

I am not a Java devloper, so take review with a grain of salt. There are a lot of unncessary comments: 3) Methods: size ...
FromTheStackAndBack's user avatar
3 votes
Accepted

Implementation of binary tree traversal

Class definition There is no need to explicitly inherit from object; that is a Python 2.x anachronism. Instead of ...
AJNeufeld's user avatar
  • 35.3k
3 votes

LeetCode: Deepest Leaves Sum C#

Rename DFS to DeepestLeavesSum. Extract depth + 1 to variable. In here ...
shanif's user avatar
  • 725
3 votes
Accepted

LeetCode: Most Frequent Subtree Sum

The function get_sums_freqs returns an int, but when I call it, I do not assign the result to anything: ...
AJNeufeld's user avatar
  • 35.3k
3 votes

Lowest Common Ancestor of a Binary Tree in Rust

...
Winston Ewert's user avatar
3 votes

Binary Expression Tree that evaluates Postfix input

Some additional notes on your code. Keep code noise low. Code noise is any code that is not required and gets in the way of maintainability and readability. Avoid single use variables. Keep names ...
Blindman67's user avatar
  • 22.9k
3 votes

Max-heap implementation in C

It strikes me that there is an opportunity for some of your functions to be very straightforward preprocessor macros. ...
Chris's user avatar
  • 5,482
3 votes

A toy implementation of binary tree

Inefficient isAVLBalanced() makes far too many excursions through the tree to report a boolean answer. It looks O(n*n). Note: AVL balanced tree Instead walk the ...
chux's user avatar
  • 36.5k
3 votes
Accepted

leetcode 530. In-order traversal of a binary search tree (C++17)

Use std::invoke() to call the visitor Visitor can be anything: a regular function, a lambda, or even a pointer to member ...
G. Sliepen's user avatar
  • 69.5k
3 votes
Accepted

Binary tree struct in C

General Observations The use of the typedef for the node struct is a good practice. The fact that all of the loops and ...
pacmaninbw's user avatar
  • 26.2k
3 votes

Maximum Sum BST in a Binary Tree

Is there anything I can change, in the same given code, that would optimize it and/or get it accepted? Your “Time Limit Exceeded” question is basically asking “when is this code slow?” We tend to ...
J_H's user avatar
  • 42.7k
3 votes

Maximum Sum BST in a Binary Tree

Beyond method naming and a comment stating what the signature boolean isBST(TreeNode root,…) suggest, the code gives no indication what it is there for, what the ...
greybeard's user avatar
  • 7,779
2 votes
Accepted

Leetcode: Subtree of Another Tree

Your code doesn't look that ugly! Here are some suggestions to make it better. Note that LeetCode makes significantly more mistakes than you when it comes to code quality; I have listed the ...
L. F.'s user avatar
  • 9,715

Only top scored, non community-wiki answers of a minimum length are eligible