AlgorithmCrackers / Interview-Questions

List of all the Interview questions practiced from online resources and books
246 stars 127 forks source link

invert a binary tree #10

Closed prakashn27 closed 4 years ago

prakashn27 commented 9 years ago

Possible solution of max Howel interview question? have to test it before integrating it. http://www.quora.com/What-is-the-algorithmic-approach-to-invert-a-given-binary-tree

santhoshvai commented 9 years ago

that solution is not too good, the one below must work. Basic recursion qn in my opinion.

void reverse(Node root) {
    if (root == null)  return; 
   Node temp = root.Left;
   root.Left = root.Right;
   root.Right = temp;
   reverse(root.Left);
   reverse(root.Right);
}
prakashn27 commented 9 years ago

where do we set the left and right tree after inversing??

santhoshvai commented 9 years ago

Lol.. i forgot to add that.. I have edited it now..

prakashn27 commented 9 years ago

we will close the issue when we implement the structure and add this code into it.

santhoshvai commented 4 years ago

See https://github.com/AlgorithmCrackers/Interview-Questions/tree/master/04_Trees_and_Graphs/invert-binary-tree