All Questions
Tagged with depth-first-search tree
26 questions
1
vote
1
answer
1k
views
How does one write DFS in python with a Deque without reversed for Trees?
I have implemented a correct version of DFS (with comments explaining the implementation):
...
2
votes
1
answer
372
views
Sum of all Paths in Binary Tree
Problem
For the given binary tree return the list which has sum of every paths in a tree. i.e Every path from root to leaf.
I've written following solution.
...
1
vote
1
answer
84
views
Depth First Search vs Breadth First Search
So after years of teaching myself, I think I'm ready to stand on the shoulders of giants and ask you a good question!
I have a tree of nodes. What I want is a function that returns an array of the ...
3
votes
1
answer
51
views
Javascript Tree Class 2
This question is the second version of the code here.
I'm writing a general tree class. Specifically, each node should have oen parent, some number of children, and hold a value.
I'm looking for ...
4
votes
2
answers
1k
views
Javascript Tree Class
I got caught in the trap of implementing my own data structure. I've created a very general tree structure, where each node gets one parent, some children, and holds a value.
I'm mostly looking for ...
2
votes
2
answers
263
views
Leetcode: Flatten binary tree to linked list C#
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
Please comment on performance
Given a binary tree, flatten it to a linked list in-place.
For example, given the following ...
2
votes
1
answer
2k
views
Binary Search Tree Implementation in Java
Looking for critiques on my BST implementation, particularly to check if I'm building the common operational methods and traversal algorithms properly. Code has been lightly tested, and I've tried to ...
0
votes
1
answer
354
views
At least 2 paths down the binary tree have the same sum
Problem statement
Write a function that given a binary tree, returns true if at least 2 paths down the tree have the same sum. A path is a series of nodes from the root to the leaf.
Examples:
...
2
votes
0
answers
413
views
BinaryTree traversed in postorder (DFS), iteratively
I was given this coding question for a technical interview:
Given a binary tree, implement a method to populate the list
post_ordered_list with the data of the nodes traversed in postorder,
...
3
votes
1
answer
2k
views
"The Story of a Tree" solved using depth-first search
Found this problem in hackerrank.
One day Bob drew a tree, with \$n\$ nodes and \$n-1\$ edges on a piece of paper. He soon discovered that parent of a node depends on the root of the tree. The ...
6
votes
1
answer
519
views
Hackerrank - Maximum Disjoint Subtree Product
Problem statement
We define an unrooted tree, T, with the following properties:
T is a connected graph with nodes ...
5
votes
1
answer
125
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 ...
3
votes
1
answer
840
views
Ternary tree preoder traversal (recursive implementation)
Problem statement:
Print out to console the ternary tree preorder traversal. Suppose that the tree can have at most 3 children, left, middle and right. Preorder traversal is to visit the middle ...
11
votes
1
answer
2k
views
Yet another minimax tree for TicTacToe
I have written a working minimax tree for Tic-Tac-Toe. What I have done so far is create a tree with end node having value 10, 0 or -10 base on win, lose or draw. After I create a tree I then ...
0
votes
1
answer
4k
views
5
votes
4
answers
5k
views
Path sum in binary tree
Root to leaf path sum equal to a given number
Given a binary tree and a number, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the given ...
6
votes
5
answers
11k
views
DFS in Binary Tree
I have written this code for DFS in a binary tree and would like improvements on it.
...
8
votes
1
answer
1k
views
Search iteratively in a ternary search tree
I was trying to write an iterative search function for my Ternary Search Tree class based on a pseudo-code, and now it seems to work, but I think it can definitely be improved.
...
8
votes
2
answers
3k
views
Equivalent binary trees (A Tour of Go)
Any suggestions on how to improve the code shown below for the Go-tour exercise?
Exercise description:
There can be many different binary trees with the same sequence of
values stored at the ...
9
votes
1
answer
1k
views
Functional tree iteration in Common Lisp
I'm adding some functionality to an existing library of data structures in Common Lisp, with a view to asking the original author if I can take over maintenance and development of it. While the ...
7
votes
2
answers
251
views
Search for Tree
Here is a search implementation for my tree. It searches each node in a breadth-first manner first, but it goes deeper in a depth-first manner, in the event it needs to go deeper. I know there may ...
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:
...
1
vote
2
answers
7k
views
DFS on a binary tree: marking nodes as visited
I wanted to write depth first search reversal iteratively in Java. Can someone review my code? I'm not that much interested in naming of the variables, but in the structure of the code, i.e. I don't ...
6
votes
3
answers
2k
views
Missing level of technical depth (Flatten Tree)
I recently have given a coding test in a reputable IT company. There were three coding questions.
They refused me by saying that
as we felt they didn't demonstrate the level of technical depth we'...
0
votes
2
answers
734
views
How to enumerate the internal nodes and leaves of a tree more elegantly?
Might there be a simpler way to write this code? I'd like to learn to write code more efficiently.
...
8
votes
2
answers
11k
views
Returning a list of the values from the binary tree
I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers?
...