zoukankan      html  css  js  c++  java
  • [LeetCode] Populating Next Right Pointers in Each Node II 解题报告


    Follow up for problem "Populating Next Right Pointers in Each Node".
    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
    » Solve this problem

    [解题思路]
    与上一题类似,唯一的不同是每次要先找到一个第一个有效的next链接节点,并且递归的时候要先处理右子树,再处理左子树。


    [Code]
    1:    void connect(TreeLinkNode *root) {  
    2: // Start typing your C/C++ solution below
    3: // DO NOT write int main() function
    4: if(root== NULL) return;
    5: TreeLinkNode* p = root->next;
    6: while(p!=NULL)
    7: {
    8: if(p->left!=NULL)
    9: {
    10: p = p->left;
    11: break;
    12: }
    13: if(p->right!=NULL)
    14: {
    15: p = p->right;
    16: break;
    17: }
    18: p = p->next;
    19: }
    20: if(root->right!= NULL)
    21: {
    22: root->right->next = p;
    23: }
    24: if(root->left !=NULL)
    25: {
    26: root->left->next = root->right? root->right:p;
    27: }
    28: connect(root->right);
    29: connect(root->left);
    30: }


    [Note]
    1. Line 6, while loop, not if
    For example,
     Level 1                      1
                                /              \
     Level 2              2               3
                         /        \                \
     Level 3       4         5                 6
                     /
     Level 4   7

    When processing Level 3, while loop is necessary for finding a valid next.


    Update 03/09/2014 Add a non-recursion version

    1:       void connect(TreeLinkNode *root) {  
    2: TreeLinkNode* cur = root, *next = NULL;
    3: while(cur!=NULL)
    4: {
    5: TreeLinkNode *p = cur, *k= NULL;
    6: while(p!=NULL)
    7: {
    8: TreeLinkNode* sub = getLinkedLeftNode(p);
    9: if(sub != NULL)
    10: {
    11: if(next == NULL)
    12: {
    13: next = sub;
    14: k = sub;
    15: }
    16: else
    17: k->next = sub;
    18: while(k->next !=NULL) // ietrate to the tail
    19: k = k->next;
    20: }
    21: p = p->next;
    22: }
    23: cur = next;
    24: next = NULL;
    25: }
    26: }
    27: TreeLinkNode* getLinkedLeftNode(TreeLinkNode * root)
    28: {
    29: if(root->left != NULL && root->right != NULL)
    30: root->left->next = root->right;
    31: if(root->left != NULL)
    32: return root->left;
    33: if(root->right != NULL)
    34: return root->right;
    35: return NULL;
    36: }


  • 相关阅读:
    [LeetCode] 875. Koko Eating Bananas 科科吃香蕉
    [LeetCode] 874. Walking Robot Simulation 走路机器人仿真
    [LeetCode] 995. Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数
    [LeetCode] 873. Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度
    [LeetCode] 872. Leaf-Similar Trees 叶结点相似的树
    [LeetCode] 870. Advantage Shuffle 优势洗牌
    [LeetCode] 869. Reordered Power of 2 重新排序为2的倍数
    [LeetCode] 868. Binary Gap 二进制间隙
    [LeetCode] 867. Transpose Matrix 转置矩阵
    [LeetCode] 866. Prime Palindrome 质数回文数
  • 原文地址:https://www.cnblogs.com/codingtmd/p/5078974.html
Copyright © 2011-2022 走看看