Skip to content

Instantly share code, notes, and snippets.

@Indy9000
Created September 8, 2016 19:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Indy9000/5959048a4064fb399b4a08d64af5d72b to your computer and use it in GitHub Desktop.
/*********
* This Gist was created at CSharpPad.com
* To run this file, open http://csharppad.com/gist/b93b925c378a4ff2a04fa5bc0e218786
**********/
class Node{
public int value;
public Node left;
public Node right;
}
Node InvertBinTree(Node root){
if (root == null){
return root;
}else{
var l = InvertBinTree(root.right);
var r = InvertBinTree(root.left);
root.left = l;
root.right = r;
return root;
}
}
Node Insert(Node node, int value){
if(node == null){
node = new Node();
node.value = value;
return node;
}else{
if(value < node.value){
node.left = Insert(node.left, value);
}else{
node.right = Insert(node.right, value);
}
return node;
}
}
void DFS(Node node){
if(node==null){
return;
}else{
DFS(node.left);
Console.Write("{0}",node.value);
DFS(node.right);
}
}
//--------
var arr = new int[]{4,2,7,1,3,6,9};
Node root = null;
for(int i=0; i<arr.Length; ++i){
root = Insert(root, arr[i]);
}
//--------
DFS(root);
//--------
root = InvertBinTree(root);
DFS(root);
//--------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment