Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
1 answer
2k views

Looping through a tree to get branch or leaf

This is my solution to a problem from MIT's 6.034 Artificial Intelligence course (see the heading Tree reference on 6.034 Artificial Intelligence, Lab 0). Can you please help me improve my solution (...
user avatar
5 votes
1 answer
742 views

Python beginner's tree with unit tests

I have implemented a tree in Python. However, due to my lack of experience with Python, I am not able to judge the overall quality of my code. ...
Boni's user avatar
  • 51
4 votes
4 answers
5k views

Check if two binary trees are equal

I've recently solved the LeetCode's "Same Tree" problem: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are ...
alecxe's user avatar
  • 17.5k
6 votes
2 answers
7k views

Prefix search by trie tree

I'm working on the prefix search problem: Given a set of words, for example words = ['a', 'apple', 'angle', 'angel', 'bat', 'bats'], for any given prefix, find ...
Lin Ma's user avatar
  • 3,543
5 votes
1 answer
232 views

Using range minimal query for lowest common ancestor (LCA) -- part 2

Here is my code to use range minimal query to resolve the LCA problem. I applied comments from here. Any advice on code bugs, performance improvement in terms of algorithm time complexity, code style ...
Lin Ma's user avatar
  • 3,543
4 votes
1 answer
525 views

Using range minimal query for lowest common ancestor (LCA)

Here is my code to use range minimal query to resolve the LCA problem. Any advice on code bugs, performance improvement in terms of algorithm time complexity, code style are appreciated. More ...
Lin Ma's user avatar
  • 3,543
2 votes
1 answer
278 views

Serialize and deserialize binary tree with a lot of empty nodes

Here is my code to serialize and deserialize binary tree, which may have a lot of empty nodes. I am written in Python 2.7. Any advice on performance improvement in terms of algorithm time complexity, ...
Lin Ma's user avatar
  • 3,543
2 votes
0 answers
632 views

Vertical sum in a given binary tree

I'm working on this problem, Problem statement, Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: ...
Lin Ma's user avatar
  • 3,543
2 votes
2 answers
3k views

Serialize and deserialize binary tree

Here is my code of serialize and de-serialize a binary tree, looking for advice. One issue in my mind which I cannot resolve is, I am using a global variable index, wondering if more elegant solutions ...
Lin Ma's user avatar
  • 3,543
3 votes
1 answer
2k views

Serialize and deserialize a tree

Suppose each node of a tree has maximum n children. I want to serialize and de-serialize for a tree. My current solution is doing pre-order traverse, and using <...
Lin Ma's user avatar
  • 3,543
16 votes
5 answers
9k views

Google Foobar Challenge: Lucky Triples

Note: Since my time has passed for this challenge, I do not remember exactly the stipulations but will attempt to recapitulate them to the best of my knowledge. Essentially, the challenge was this: ...
Edwin Rice's user avatar
2 votes
1 answer
191 views

Find the shortest whole repetitive substring (part 2)

This is a continued improvement from discussion here (Find the shortest whole repetitive substring), since code has changed a lot dues to improvement, I post it here. Major smart ideas are from ...
Lin Ma's user avatar
  • 3,543
7 votes
1 answer
3k views

Trie Tree match string containing ? and *

Using Python 2.7 and here is my code which implement trie match for a whole word, and also support if the whole word contains ? (match any one character) or ...
Lin Ma's user avatar
  • 3,543
1 vote
1 answer
12k views

Print tree level by level including Empty node

Here is my code to print tree in a level by level way. It needs to print an empty node as well to keep tree structure like full binary tree for elegancy reasons. I'm wondering if anything you think ...
Lin Ma's user avatar
  • 3,543
5 votes
2 answers
599 views

Print tree in a zigzag manner

I want to print a tree in a zigzag manner level-by-level. I'm wondering if there are any logic issues in my code and any further performance improvement ideas by using a smarter algorithm ...
Lin Ma's user avatar
  • 3,543

15 30 50 per page