Leetcode: Populating Next Right Pointers in Each Node II

来源:互联网 发布:淘宝修改折扣价权重 编辑:程序博客网 时间:2024/05/19 07:07

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

按层次遍历可以继续用,不过不是constant extra space.

/** * 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) {        if (root == NULL) return;                    TreeLinkNode *cur = NULL;          queue<TreeLinkNode*> trees;          trees.push(root);          trees.push(NULL);          while (!trees.empty()) {              cur = trees.front();              trees.pop();              if (cur != NULL) {                  cur->next = trees.front();                  if (cur->left != NULL) {                      trees.push(cur->left);                }                if (cur->right != NULL) {                    trees.push(cur->right);                  }              }              else if (!trees.empty()) {                  trees.push(NULL);              }          }      }};

对应I的第二方法:

class Solution {public:    void connect(TreeLinkNode *root) {        if (root == NULL || root->left == NULL && root->right == NULL) {              return;          }                  connect(root->left);          connect(root->right);                  int level = 0;        while (true) {            TreeLinkNode *most_right = mostRight(root->left, level);            TreeLinkNode *most_left = mostLeft(root->right, level);            if (most_right != NULL && most_left != NULL) {                most_right->next = most_left;                ++level;            }            else {                break;            }        }     }        TreeLinkNode *mostRight(TreeLinkNode* root, int level) {        if (root == NULL) {            return NULL;        }        else if (level == 0) {            return root;        }                TreeLinkNode *right = mostRight(root->right, level - 1);        if (right == NULL) {            right = mostRight(root->left, level - 1);        }                return right;    }        TreeLinkNode *mostLeft(TreeLinkNode* root, int level) {        if (root == NULL) {            return NULL;        }        else if (level == 0) {            return root;        }                TreeLinkNode *left = mostLeft(root->left, level - 1);        if (left == NULL) {            left = mostLeft(root->right, level - 1);        }                return left;    }};
对应I的第三种方法,基本是从http://blog.csdn.net/fightforyourdream/article/details/16854731 借鉴而来。学习了。
class Solution {public:    void connect(TreeLinkNode *root) {        if (root == NULL || root->left == NULL && root->right == NULL) {              return;          }                  TreeLinkNode *rnext = root->next;        TreeLinkNode *next = NULL;        while (rnext != NULL && next == NULL) {            if (rnext->left != NULL) {                next = rnext->left;            }            else if (rnext->right != NULL) {                next = rnext->right;            }            rnext = rnext->next;        }                if (root->left != NULL) {            if (root->right != NULL) {                root->left->next = root->right;            }            else {                root->left->next = next;            }        }        if (root->right != NULL) {            root->right->next = next;        }                connect(root->right);        connect(root->left);      }};

===================第二次=================

/** * 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) {        if (root == NULL || root->left == NULL && root->right == NULL) {            return;        }                TreeLinkNode* childNext = NULL;        TreeLinkNode* next = root->next;        while (childNext == NULL && next != NULL) {            if (next->left != NULL) {                childNext = next->left;            }            else if (next->right != NULL) {                childNext = next->right;            }            next = next->next;        }                if (root->left != NULL) {            root->left->next = root->right ? root->right : childNext;        }        if (root->right != NULL) {            root->right->next = childNext;        }                connect(root->right);        connect(root->left);    }};


0 0
原创粉丝点击