您的位置:首页 > Web前端 > Node.js

****(leetcode) Populating Next Right Pointers in Each Node II

2014-11-23 11:06 316 查看
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


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


1、根据完全二叉树方法修改判断语句即可! 开始一直超时,跟着程序跑了一遍才发现一直conn2没有在每次都设为NULL。 思考不细致啊!!

/**
 * 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 *firstNodeEveryRow, *parent, *conn1,*conn2;  
        firstNodeEveryRow=root; //first..每一行的第一个节点  
        while(NULL != firstNodeEveryRow){  
            parent = firstNodeEveryRow;  
            conn1 = conn2=NULL; //conn1->next=conn2, 用于连接  
            while(NULL != parent){ //用于遍历改行的节点  
                if(parent->left != NULL&&parent->right != NULL)     //左右兄弟先连接  
                    parent->left->next = parent->right;  
                if(parent->left != NULL || parent->right != NULL)  
                    conn2 = (parent->left != NULL)?parent->left:parent->right;  
                if(conn1!=NULL&&conn2!=NULL){   //两个父节点之间的子节点相连接  
                     conn1->next=conn2;  
                     conn1=NULL;  
                }  
               
                if(conn1==NULL&&(parent->left!=NULL||parent->right!=NULL))  
                    conn1=(parent->right!=NULL)?parent->right:parent->left;  
                parent=parent->next;
                conn2=NULL; //!conn2表示的当前parent节点里面的用于和前面节点相连接的点,so parent更换后conn2要置为NULL,不然会成环!!
            }  
            while(firstNodeEveryRow != NULL && firstNodeEveryRow->left==NULL && firstNodeEveryRow->right==NULL)  
                firstNodeEveryRow=firstNodeEveryRow->next;  
            if(NULL != firstNodeEveryRow)  
                firstNodeEveryRow=(firstNodeEveryRow->left != NULL)?firstNodeEveryRow->left:firstNodeEveryRow->right;  
        }  }
};


2、递归:

/**
 * 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 *rt=NULL, *nextNode=NULL;
        if(NULL==root)
            return;
        rt = root->next;
        while(rt!=NULL){
            if(rt->left!=NULL){
                nextNode = rt->left;
                break;
            }
            if(rt->right!=NULL){
                nextNode = rt->right;
                break;
            }
            rt=rt->next;
        }
        if(root->left!=NULL){
            if(root->right!=NULL)
                root->left->next = root->right;
            else
                root->left->next = nextNode;
        }
        if(root->right!=NULL){
            root->right->next = nextNode;
        }
        connect(root->right); //注意,先递归右侧,不然如果先递归左侧,右侧的还没有连接起来,左侧递归时无法连接
        connect(root->left);  // 因为左侧的连接需要右侧的信息
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: