Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created October 22, 2017 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/610c2969f9b3433e3e51c5ce0ae2b831 to your computer and use it in GitHub Desktop.
Save jianminchen/610c2969f9b3433e3e51c5ce0ae2b831 to your computer and use it in GitHub Desktop.
upside down a binary tree - actually a complete binary tree
/*
1 2 3 2 -- 3
|
1
1
1
/ \
2 3 ---
2
/ \
1 3
0 - node
1
1 - 2, 3
2 - 4 ,5
1
/ \
2 3 ---
// \
\4 - 5
- we have to store nodes by each level - node 3 levels 2 - , 4, 5, four node: 4, 5, 6, 7, 3 is parent for 6 7, iterate, visit4, 6 handle -> 2
compelete the level 2, a loop, left order, left, "", mark ""
root = 4
4
/ \
2 5
/ \
1 3
1. Find root node, depending on level - count level - maximum level -> determine leftmost node in the maximum level -> root node
2. Rebuild the relationship, if the nodeN is left child of node levelNMinus1, then levelMinus1 is right child of NodeN
and then levelNMinus1's right child is to be left child of NodeN.
*/
public class Node
{
public int Value {get; set;}
public Node Left {get; set; }
public Node Right {get; set;}
// public Node Parent {get; set;}
}
///
/// root node of upside down is by maximum level left most node
/// using recursive call
/// walk through test case 1, 2, 3
// walk through test case 1, 2, 3, 4, 5
// walk through test case 1, 2, 3, 4, 5, 6, 7
// 1
// 2 3
// 4 5 6 7 -> line 91 for loop, iterate 4, 5, 6, 7, need to check 4 and 6
public static Node UpsideDownCompleteBinaryTree(Node root) // 1, 2,3 // 1, 2,3, 4, 5, 6, 7
{
if(root.IsNull)
{
return null;
}
///
var maximumLevel = GetMaximumLevel(root); // level 0, 1, -> 2, 2
var levelList = new List<Node>[maximumLevel]; // left to right // leveList[0] - 1, levelList[1] = {new Node(2), new Node(3),} , LeveList[2] = new Node[]{new Node(4), new Node(5), new Node(6), new Node(7)}
iterateTree(root, levelList); // DFS, BFS - recursive , simple and easy ->
/// Go over the maximum level - bottom up - while loop - recursive
/// go over each level, from bottom up -> go over each node which is left child -> do reverse relationship
var indexLevel = maximuLevel - 1; // 2 start from 1
var newRoot = null;
var setNewRoot = false;
while (indexLevel > 0)
{
var isMaximumLevel = indexLevel == maximumLevel; // true, true
var currentList = levelList[index]; // levelList[1], same
var currentLength = currentList.Length; // 2, 4
if(isMaximuLevel && !SetNewRoot) // true, true, true
{
newRoot = currentList [0]; // levelList[1][0], find root
SetNewRoot = true; // do not set again
}
// go over the each node in the level, only check node which is left child
//int numberOfNode = level; // ? comment out
for(int i = 0; i < currentLength; i++) // 0, handle , 4, 5,6
{
[1,2,3]
var current = currentList [i]; // visit current list from left to right -> levelList[1][0] = new Node(2) , levelList[2][0] = new Node(4)
var parentLevel = indexLevel - 1; // 0, 1
var parentNodeIndex = i / 2; // test case: // 0 1 - > 0 , 2 , 3 -> 1, 0 , 0
var parentNode = LevelList[parentLevel][parentNodeIndex]; // new Node(1) LevelList[1][0] = new Node(2)
var isLeftChild = parentNode.Left == current; // true
if(!isLeftChild)
{
continue; // 5 will go here,
}
// ready to do reverse, delete right child node relationship
// two steps:
current.Right = parentNode.right; // current newNode(2) -> Node(1).right = Node(3), 4's right = 5
current.Left = parentNode; //1, 4's left = 2
// clean up - Node 1's left and right child ->
parentNode.Left = null; // 2's left = null
parentNode.right = null; // 2's right = null
}
indexLevel --;
}
return newRoot;
}
@jianminchen
Copy link
Author

jianminchen commented Oct 22, 2017

Feedback from the peer:

  1. Go to whiteboard testing without the peer's remind
  2. Comment style, make it more short, readable
  3. Easy to understand/ easy to do
  4. Time and space complexity analysis, ask the input from the peer, and make an agreement first before coding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment