dfs

[Leetcode 655] Print Binary Tree

Question 655. Print Binary Tree Print a binary tree in an m*n 2D string array following these rules: The row number m should be equal to the height of the given binary tree. The column number n should always be an odd number. The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part).

[Leetcode 117] Populating Next Right Pointers in Each Node II

Question 117. Populating Next Right Pointers in Each Node II Given a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Follow up: You may only use constant extra space.

[Leetcode 785] Is Graph Bipartite

Question Leetcode 785: Is Graph Bipartite? Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B. The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists.

Create tree from tuples

This is a coding problem from Uber onsite interview found here. Question: Given a list of pairs (tuples), each of them represents a connection from parent to it’s child in a tree. In this list, nodes will have edges not only for their direct children, but also all of it’s descendants (i.e. children’s children, and their children etc). If we have the following tree as an example: A / \ B C / \ D E Then the list given it will be