Populating Next Right Pointers in Each Node II

原创 2015年11月21日 00:43:24

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


思路:

这一题的思路更为巧妙,我觉得现在尤其注意一点 ,在链表开头加上一个空链表是一个非常好的方法。

对于本题,我似乎忽视的一点就是,对于当前层次的节点,是默认为右边是链接好的,而之前的一个类似题目,是判断当前节点的next 的了的。这个是区别。

具体思路是:在每一层之前加上一个dummy ,如果当前节点存在左孩子,dummy 指向左孩子,存在右孩子,tail 首先指向 下一个,再指向右孩子。直到当前节点为空。

接下来就是之前的dummy 设为当前节点,如此循环反复。

代码:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        TreeLinkNode *dummy=new TreeLinkNode(0);
        
        if(root==NULL)  return;
        
        while(root!=NULL){
            TreeLinkNode *tail=dummy;
            while(root!=NULL){
                if(root->left){
                    tail->next=root->left;
                    tail=tail->next;
                }
                if(root->right){
                    tail->next=root->right;
                    tail=tail->next;
                }
                root=root->next;
            }
            root=dummy->next;
            dummy->next=NULL;//如果没有这句话,那么dummy只是上面那一层的
        }
        delete dummy;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JSRGFJZ/article/details/49955833

Populating Next Right Pointers in Each Node -- LeetCode

原题链接: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/  这道题是要将一棵树的每一层维护...
  • linhuanmars
  • linhuanmars
  • 2014-04-12 03:11:29
  • 10417

LeetCode:Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II Total Accepted: 63428 Total Submissions: 192670 ...
  • itismelzp
  • itismelzp
  • 2016-06-14 15:21:29
  • 659

[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...
  • qq508618087
  • qq508618087
  • 2016-01-13 06:36:40
  • 1023

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 could b...
  • fly_yr
  • fly_yr
  • 2015-11-14 15:13:02
  • 890

Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode

这道题有些tricky,有时间回头再研究一下 package Level4; import Utility.TreeLinkNode; /** * Populating Next Right...
  • hellobinfeng
  • hellobinfeng
  • 2013-11-21 04:45:02
  • 3459

117. 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 ...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-26 10:16:40
  • 566

[LeetCode]Populating Next Right Pointers in Each Node II, 解题报告

前言 今天上午从9点半开始就做了这么一道题目,还是挺有意思的,这里记录一下 题目 Follow up for problem "Populating Next Right Pointer...
  • zinss26914
  • zinss26914
  • 2013-12-19 11:17:47
  • 7643

【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】

【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Giv...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-12 06:30:46
  • 3137

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

Populating Next Right Pointers in Each Node Total Accepted: 44163 Total Submissions: 122231Given a ...
  • a363344923
  • a363344923
  • 2015-03-30 20:44:13
  • 1452

Populating Next Right Pointers in Each Node II -- LeetCode

原题链接: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/  这道题目的要求和Popul...
  • linhuanmars
  • linhuanmars
  • 2014-04-13 09:09:24
  • 9184
收藏助手
不良信息举报
您举报文章:Populating Next Right Pointers in Each Node II
举报原因:
原因补充:

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