Populating Next Right Pointers in Each Node II [leetcode] 空间O(1)的基于循环和基于递归的两种方法

基于循环的方法:

    void connect(TreeLinkNode *root) {
        if (root == NULL) return;
        TreeLinkNode * start = root;
        TreeLinkNode * end = root;
        TreeLinkNode * levelEnd = root;
        while (start != NULL)
        {
            if (start->left != NULL)
            {
                end->next = start->left;
                end = end->next;
            }
            if (start->right != NULL)
            {
                end->next = start->right;
                end = end->next;
            }
            if (start == levelEnd)
            {
                start = start->next;
                levelEnd->next = NULL;
                levelEnd = end;
            }
            else
            {
                start = start->next;
            }
        }
    }

基于递归的方法

    void connect(TreeLinkNode *curQueue) {
        if (!curQueue) return;
        TreeLinkNode* nextQueue = new TreeLinkNode(-1);//dummy node
        TreeLinkNode* head = nextQueue;
        while (curQueue)
        {
            if (curQueue->left)
            {
                nextQueue->next = curQueue->left;
                nextQueue = nextQueue->next;
            }
            if (curQueue->right)
            {
                nextQueue->next = curQueue->right;
                nextQueue = nextQueue->next;
            }
            curQueue = curQueue->next;
        }
        nextQueue = head;
        head = head->next;
        delete nextQueue;
        connect(head);
    }
时间: 2024-12-29 19:50:22

Populating Next Right Pointers in Each Node II [leetcode] 空间O(1)的基于循环和基于递归的两种方法的相关文章

Populating Next Right Pointers in Each Node II leetcode java

题目: 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 bina

Populating Next Right Pointers in Each Node II LeetCode

Populating Next Right Pointers in Each Node II Total Accepted: 18934 Total Submissions: 62031My Submissions Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous

Populating Next Right Pointers in Each Node II Leetcode Python

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 t

【57】117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Description Submission Solutions Add to List Total Accepted: 84339 Total Submissions: 251330 Difficulty: Medium Contributors: Admin Follow up for problem "Populating Next Right Pointers in Each Node

【leetcode】Populating Next Right Pointers in Each Node II

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 s

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

Populating Next Right Pointers in Each Node IIFollow 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 sp

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 t

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

#Leet Code# Populating Next Right Pointers in Each Node II

描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: 1 class Solution: 2 # @param root, a tree node 3 # @return nothing 4 def doSth(self, nextNode, conNode): 5 while nextNode is not