Populating Next Right Pointers in Each Node II--leetcode難題講解系列

Given a binary treenode

    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.ide

Initially, all next pointers are set to NULL.函數

Note:spa

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

 

For example,
Given the following perfect binary tree,3d

         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

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/code

因爲空間複雜度爲O(1),廣搜深搜不能使用,只能考慮遞歸迭代。充分利用parent的next指針,咱們能夠很容易的找到子節點的next指針。blog

// C++ RECURSIVE CODE: 
class
Solution { public: static void connect(TreeLinkNode* root){ if(root == NULL) return; if(root->left) root->left->next = root->right; if(root->next && root->right ) root->right->next = root->next->left; connect(root->left); connect(root->right); } };

 實際上用棧也是會消耗空間的,迭代應該是最合適的辦法。爲了保持處理的一致性,咱們能夠給每一層加一個dummy頭結點,而後根據parent層(利用next)決定child層的next。遞歸

// JAVA ITERATIVE CODE:
public
class Solution { public void connect(TreeLinkNode root) { TreeLinkNode dummy = new TreeLinkNode(0); while(root != null){ TreeLinkNode child = dummy; dummy.next = null; while(root != null){ if(root.left != null){ child.next = root.left; child = child.next; } if(root.right != null){ child.next = root.right; child = child.next; } root = root.next; } root = dummy.next; } } }
PYTHON ITERATIVE CODE:
class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        dummychild = TreeLinkNode(0)
        while root:
            cur = dummychild
            dummychild.next = None
            while root:
                if root.left:
                    cur.next = root.left
                    cur = cur.next
                if root.right:
                    cur.next = root.right
                    cur = cur.next
                root = root.next
            root = dummychild.next
View Code
 
 
 

 

 

 

Follow up for problem "Populating Next Right Pointers in Each Node".leetcode

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

 

For 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
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/


惟一的區別是找子節點的next指針不是那麼直接了,能夠用函數根據不一樣狀況來返回,其餘的部分作法同樣。
class Solution { public: TreeLinkNode* first(TreeLinkNode* root){ root = root->next; while(root){ if(root->left) return root->left; else if(root->right) return root->right; root = root->next; } return NULL; } void connect(TreeLinkNode* cur){ TreeLinkNode* root = cur; if(root == NULL) return; while(root){ if(root->left){ root->left->next = root->right ? root->right: first(root); } if(root->right){ root->right->next = first(root); } root = root->next; } connect(cur->left); connect(cur->right); } };
相關文章
相關標籤/搜索