Skip to main content

All Questions

Tagged with
2 votes
1 answer
128 views

Find ALL duplicate subtrees. using recursion

This is just a practice exercise, I'm trying to understand dynamic programming as deeply as I can. I solved this using recursion, though I am not sure if it will always work. If anyone could tell me a ...
beatmaister's user avatar
3 votes
0 answers
292 views

Noughts and Crosses Terminal Game with AI

I created a game which can play tic-tac-toe with: 0 players (AI vs AI), 1 player (human vs AI) or 2 player (human vs human). Here is the AI function which wraps around my innermost function. This ...
RelativelyUnique's user avatar
5 votes
1 answer
1k views

Max Sum of Nodes in Each Path in Binary Tree

I wanted to get a review on an algorithm I wrote for a binary tree problem. The problem is the following. Return the maximum sum between all branches in a binary tree. A branch is defined as all ...
John Lane's user avatar
3 votes
1 answer
6k views

Recursive BFS solution for tree traversal

I'm somewhat new to the wonderful world of Python, outside of simple codecademy-like challenges online. This function was part of a project for school. It's tested and works, but I'm looking for some ...
wanderbread's user avatar
2 votes
1 answer
2k views

Given a binary tree, find the maximum path sum

This is a leetcode.com problem. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must ...
Stack crashed's user avatar
2 votes
1 answer
135 views

Time complexity of common ancestor algorithm

I have an algorithm here to find the common ancestor of two nodes in a binary tree. ...
redixhumayun's user avatar
5 votes
1 answer
129 views

Given a binary tree, \$T\$ whose nodes have positive values, find the value of the maximal path

Problem Problem is Maximum sum path from https://firecode.io Given a binary tree, \$T\$, whose nodes have positive values find the value of a maximal path. A path is a simple path from a node to ...
josfervi's user avatar
  • 167
8 votes
1 answer
771 views

Convert Iterable tree to string

I code this function to convert list of dict to a specific string to export last in a row table string field. ...
GeoStoneMarten's user avatar
3 votes
1 answer
1k views

Request for memory optimizations for solution to Google FooBar challenge, "ion_flux_relabeling,"

I just finished one of the Google FooBar Challenges.My Python is super rusty. I did the challenge in Haskell first and then attempted to covert the code. I am guessing that led to some bad Python ...
LambdaScientist's user avatar
3 votes
1 answer
917 views

Printing the hierarchical representation, and finding all ascendants for a node, in a self-referential table in Python with recursion

I have a self referential database table: CREATE TABLE category ( id INT PRIMARY KEY, name VARCHAR, parent_id INT REFERENCES category (id) ); And ...
Matthew Moisen's user avatar
4 votes
3 answers
1k views

Python function to determine whether a given BST is valid

Here's a simple function I wrote using recursion. Does this run in \$O(n)\$ time and use \$O(n)\$ space, where \$n\$ is the number of nodes in the tree? ...
Daniel Jacobson's user avatar
1 vote
2 answers
4k views

Using recursion to count nodes in a binary tree, test equality of linked lists, and find extrema of a linked list

I am working with a some small functions to test recursion. I am fairly new to Python and am wondering if there is a cleaner way to get the job done. ...
LucyBen's user avatar
  • 273
6 votes
3 answers
7k views

Tree structure with support for inorder and preorder traversal

I am learning Tree Data Structures and came up with this code for implementing basic Tree Node using class and performing various Tree based operations like Traversals and others. The code works ...
ajknzhol's user avatar
  • 461
2 votes
2 answers
1k views

Parse a tree & return the various paths

Input: { 1: [2,3], 2:[4], 3: [5,6], 4:[6] } Expected output: [[1, 2, 4, 6], [1, 3, 5], [1, 3, 6]] My Code: ...
rtindru's user avatar
  • 153
6 votes
1 answer
977 views

Functional, but not recursive, tree traversal

To learn tree traversal i implemented os.walk, tail-recursive and stack-based. Unfortunately Python doesn't support tail call optimization. How do i rewrite my ...
Mirzhan Irkegulov's user avatar