public TreeNodeNextPointer(int data) { value = data; left = null; right = null; next = null; }
/// <summary> /// Given a binary tree // struct TreeLinkNode // { // TreeLinkNode* left; // TreeLinkNode* right; // TreeLinkNode* next; // } // Populate each next pointer to point to its next right node.If there is no next right node, the next pointer should be set to NULL. // Initially, all next pointers are set to NULL. // Note: //You may only use constant extra space. //Recursive approach is fine, implicit stack space does not count as extra space for this problem. //Example: //Given the following binary tree, // 1 // / \ // 2 3 // / \ \ //4 5 7 //After calling your function, the tree should look like: // 1 -> NULL // / \ // 2 -> 3 -> NULL // / \ \ //4-> 5 -> 7 -> NULL /// </summary> /// <param name="root"></param> public static void connectNodesAtSameLevel(TreeNodeNextPointer root) { if (root == null) { return; } TreeNodeNextPointer curr = root; TreeNodeNextPointer prev = root; while (prev.left != null) { // here curr is top level node, which will help to connect next level left // to connect to next level right like curr = prev; while (curr.next != null) { // 1 - here curr is 1 ( when loop is running on first iteration) // 2 3 - // 4 5 6 7 // curr -> 2 // curr.left -> 4 connecting nex to right // curr.right -> 5 (connecting 4 -> 5) curr.left.next = curr.right; // now connect 5 -> 6 // curr.right -> 5 // curr -> 2 // curr.next -> 3 (we have already connected 2 -> 3 in previous level) // curr.next.left = 6 (below line will connecr 5 -> 6) curr.right.next = curr.next.left; // now curr will move to 3 curr = curr.next; } // 1 - here curr is 1 ( when loop is running on first iteration) // 2 3 - //curr -> 1 // curr.left -> 2 connecting next to right // curr.right -> 3 (connecting 2->3) curr.left.next = curr.right; // move to next level start prev = prev.left; } }
/// <summary> /// Given a binary tree // struct TreeLinkNode // { // TreeLinkNode* left; // TreeLinkNode* right; // TreeLinkNode* next; // } // Populate each next pointer to point to its next right node.If there is no next right node, the next pointer should be set to NULL. // Initially, all next pointers are set to NULL. // Note: //You may only use constant extra space. //Recursive approach is fine, implicit stack space does not count as extra space for this problem. //You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). //Example: //Given the following perfect binary tree, // 1 // / \ // 2 3 // / \ / \ //4 5 6 7 //After calling your function, the tree should look like: // 1 -> NULL // / \ // 2 -> 3 -> NULL // / \ / \ //4->5->6->7 -> NULL // </summary> // <param name="node"></param> public static void connectNodesAtSameLevelBinaryTreeDFS(TreeNodeNextPointer root) { if (root == null) { return; } Queue <TreeNodeNextPointer> queue = new Queue <TreeNodeNextPointer>(); queue.Enqueue(root); while (queue.Count != 0) { int count = queue.Count; TreeNodeNextPointer prev = null; for (int i = 0; i < count; i++) { TreeNodeNextPointer curr = queue.Dequeue(); if (curr.left != null) { queue.Enqueue(curr.left); } if (curr.right != null) { queue.Enqueue(curr.right); } if (prev != null) { prev.next = curr; } prev = curr; } } }