[LeetCode] 117. Populating Next Right Pointers in Each Node II

原创 2016年08月30日 12:45:34

思路:
BFS. 拿三个节点, head表示下一层的头, prev表示下一层上次遍历到了谁, cur是当前层正在遍历的点.

void connect(TreeLinkNode *root) {
    TreeLinkNode* head = root;
    TreeLinkNode* prev = NULL;
    TreeLinkNode* cur = NULL;

    while (head) {
        cur = head;
        prev = NULL;
        head = NULL;

        while (cur) {
            if (cur->left) {
                if (! head)
                    head = cur->left;
                else
                    prev->next = cur->left;
                prev = cur->left;
            }
            if (cur->right) {
                if (! head)
                    head = cur->right;
                else
                    prev->next = cur->right;
                prev = cur->right;
            }
            cur = cur->next;
        }
    }
}

相关文章推荐

Leetcode 117. Populating Next Right Pointers in Each Node II 链接邻居2 解题报告

1 解题思想这道题和116基本一样: Leetcode 116. Populating Next Right Pointers in Each Node 链接邻居 解题报告 区别在于这个是任意二叉树...
  • MebiuW
  • MebiuW
  • 2016年09月30日 22:52
  • 1149

[LeetCode]problem 117. Populating Next Right Pointers in Each Node II

TAG层序遍历 - 常量空间,通过next指针二叉树link方法有了Populating next right pointers in each node I的铺垫,这道题就显得没有任何思维上的限制了...

Populating Next Right Pointers in Each Node II - LeetCode 117

题目描述: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree co...
  • bu_min
  • bu_min
  • 2015年05月23日 20:54
  • 203

LeetCode 117 Populating Next Right Pointers in Each Node II

题目 Given a binary tree struct TreeLinkNode { TreeLinkNode...
  • seawade
  • seawade
  • 2014年03月05日 12:52
  • 536

[leetcode] 117. Populating Next Right Pointers in Each Node II 解题报告

题目链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for p...

leetcode_question_117 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 b...
  • doc_sgl
  • doc_sgl
  • 2013年09月22日 22:26
  • 1207

[LeetCode]117.Populating Next Right Pointers in Each Node II

【题目】 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree ...

LeetCode 117: Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node IIFollow up for problem “Populating Next Right Pointers ...

Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II

116题引申,对于任意树填充next指针

[LeetCode 117] Populating Next Right Pointers in Each Node II

题目链接:populating-next-right-pointers-in-each-node-ii 相同题型:Populating Next Right Pointers in Each...
  • ever223
  • ever223
  • 2015年03月26日 09:57
  • 461
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode] 117. Populating Next Right Pointers in Each Node II
举报原因:
原因补充:

(最多只允许输入30个字)