## Programming Challenge – Leetcode: Flatten the binary tree to the linked list C #

Reduce a given binary tree to a linked list.

For example with the following tree:

                1
/
2 5
/
3 4 6


The flattened tree should look like this:

1

2

3

4

5

6

using system;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

{
[TestClass]
{
[TestMethod]
{
TreeNode root = new TreeNode (1);
root.left = new TreeNode (2);
root.right = new TreeNode (5);
root.left.left = new TreeNode (3);
root.left.right = new TreeNode (4);
root.right.right = new TreeNode (6);
Flattening (root);
Assert.AreEqual (1, root.data);
Assert.AreEqual (2, root.right.data);
Assert.AreEqual (3, root.right.right.data);
Assert.AreEqual (4, root.right.right.right.data);
Assert.AreEqual (5, root.right.right.right.right.data);
Assert.AreEqual (6, root.right.right.right.right.right.data);

}
public void flatten (TreeNode root)
{
if (root == null || (root.left == null && root.right == null))
{
Return;
}
stack Stack = new stack();
stack.Push (root);
while (stack.Count> 0)
{
var curr = stack.Pop ();
if (curr! = root) // In the first iteration, we do not want to move the head to the next element
{
}
if (curr.right! = null)
{
stack.Push (curr.right);
curr.right = null;
}
if (curr.left! = null)
{
stack.Push (curr.left);
curr.left = zero;
}
}
}
}
}


## Programming Languages ​​- Why does the C # pattern comparison use a different variable-scope behavior than traditional switch blocks?

Conventional switch blocks have a scope, so the following is the compiler error "A local variable or function named" message "is already defined in this scope":

Switch (value)
{
Case 1:
string message = "Val: 1";
break;
Case 2:

string message = "Val: 2";
break;
}


As Eric Lippert states:

A reasonable question is, "Why is not that legal?" A reasonable answer
is "well, why should it be"? You can have it in two ways. Either
that is legal:

Switch (y)
{
Case 1: int x = 123; ... break;
Case 2: int x = 456; ... break;
}


or that is legal:

Switch (y)
{
Case 1: int x = 123; ... break;
Case 2: x = 456; ... break;
}


but you can not have it in both directions. The designers of C # chose the second one
Way, it seems, to be the more natural way to do it.

There are other good explanations like these:

I think a good reason is that in every other case the scope of a
"Normal" local variable is a block bounded by curly braces ({}).

Why does scoping then behave differently with a switch block for the type pattern adaptation?

Animal p = new dog ();

Switch (p)
{
case dog a:
break;
Case Cat a: // Why is this legal?
break;
}


## prolog – What is the connection between logic and logic programming?

As I understand it, logic (ie, a certain theory of logic with some theoretical axioms and some possible assignments of variables) describes a particular world (in the non-modal case) or a series of related worlds (in the modal case) and their derivation Simply explore this one world (this set of related worlds) and discover / limit the possible values ​​of the variables (SAT problem).

But logic programming with its rather confusing implication relation (I still do not understand how the implication of logic programming is related to the implication context in logic – these seem to be two completely different concepts and different signs – am I right?) Allows traveling between worlds ( in the modal case – between sets of related worlds): If variables {x} have values ​​in one world, these variables {x} have different values ​​in the other world. And it's the semantics of logic programming, that set of clauses that allow such travel between worlds, in the end the traveler lands in a world that is a fixture to that set of clauses (of logic programming instructions).

The calculation is a state transition: from one state to another, from one world state / one world to another world state / another world. But logical deduction is only the exploration of a world – be it the discovery of new relationships between variables or statements (discovery of theorems) or the discovery of the consistent set of assignments to the variables (discovery of the solution to the SAT problem).

I'm right? What else can be added to this understanding between logic and logic programming?

Maybe there is a logic that allows to derive the logic programs? To start thinking about the logic programs?

## Limits and Convergence – Linear programming with a convergent coefficient

Thank you for giving MathOverflow an answer!

But avoid

• Make statements based on opinions; Cover them with references or personal experience.

Use MathJax to format equations. Mathjax reference.

## Java Enterprise programming skills that can be helpful in game development?

I just started a new job in a business software development company (pretty much the only IT entry job I live in) inevitably switching to gamedev? The company itself works with Java Stack (Spring, Tomcat, Cassandra DB) and Docker, while to the best of my knowledge the game development is done in C #, C ++ or engine-specific scripting languages ​​and may deviate the required / preferred knowledge the business world. I strive to be a game designer someday, but do not shy away from more technical parts.

## CRUD object-oriented programming

Hello

I'm a bit confused about how we persist in an object-oriented programming framework.

I'm not sure how to explain it, imagine the Person object and, for example, through inheritance create the Teacher object that inherits the person's base.

In this case, do I create two tables, one for a person and one for a teacher?

## Which programming languages ​​should be studied for a good job in information technology or computer science?

Thank you for sending a reply to Computer Science Stack Exchange!

But avoid

• Make statements based on opinions; Cover them with references or personal experience.

Use MathJax to format equations. Mathjax reference.

## c ++ – Competitive Programming Problem: Draw lines according to the specific condition

I have solved a coding problem in CodeChef WTBTR.

We have an infinitely planar Cartesian coordinate system on which N points are plotted. Cartesian coordinates of the point i are represented by (Xi, Yi). Now we want to draw (N-1) line segments that can have any lengths and the points do not have to be on the lines. The slope of each line must be 1 or -1.

Let's say the minimum distance we need to go from point I to a line of Di, and say a = max (D1, D2, D3, …, DN). We want this distance to be as small as possible.

Therefore, we need to draw lines so that they are & # 39; a & # 39; minimize and calculate a * sqrt (2).

Time limit: 1 sec

My implementation:

#include
#include
using namespace std;

int main ()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
Double-X[n]y[n];

int i;
for (i = 0; i> x[i]>> y[i];
}

Double a[n], B[n];
for (i = 0; i <n; i ++)
{
ON[i] = y[i] - x[i];
B[i] = y[i] + x[i];
}

sort (A, A + n);
sort (B, B + n);

Doppeldiff[n];

for (i = 0; i <n-1; i ++)
{
diff[i] = (A[i+1] - ON[i]);
}

double amine = 9999999999;
for (i = 0; i diff[i])
Amine = diff[i];
}

for (i = 0; i <n-1; i ++)
{
diff[i] = (B[i+1] - B[i]);
}

double Bmin = 9999999999;
for (i = 0; i diff[i])
Bmin = diff[i];
}

Double ans = min (abs (amine), abs (Bmin));
ans / = 2;
cout << ans << endl;
}
return 0;
}


My approach gives the right answer for small values ​​of N and (x, y), but gives the wrong answer for large values ​​and the time limit exceeded for others.