Created
September 8, 2016 19:55
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* | |
* 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