All Questions
15 questions
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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.
...
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 ...
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 ...
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?
...
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.
...
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 ...
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:
...
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 ...