zoukankan      html  css  js  c++  java
  • [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 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
    » Solve this problem

    [解题思路]
    与上一题类似,唯一的不同是每次要先找到一个第一个有效的next链接节点,并且递归的时候要先处理右子树,再处理左子树。


    [Code]
    1:    void connect(TreeLinkNode *root) {  
    2: // Start typing your C/C++ solution below
    3: // DO NOT write int main() function
    4: if(root== NULL) return;
    5: TreeLinkNode* p = root->next;
    6: while(p!=NULL)
    7: {
    8: if(p->left!=NULL)
    9: {
    10: p = p->left;
    11: break;
    12: }
    13: if(p->right!=NULL)
    14: {
    15: p = p->right;
    16: break;
    17: }
    18: p = p->next;
    19: }
    20: if(root->right!= NULL)
    21: {
    22: root->right->next = p;
    23: }
    24: if(root->left !=NULL)
    25: {
    26: root->left->next = root->right? root->right:p;
    27: }
    28: connect(root->right);
    29: connect(root->left);
    30: }


    [Note]
    1. Line 6, while loop, not if
    For example,
     Level 1                      1
                                /              \
     Level 2              2               3
                         /        \                \
     Level 3       4         5                 6
                     /
     Level 4   7

    When processing Level 3, while loop is necessary for finding a valid next.


    Update 03/09/2014 Add a non-recursion version

    1:       void connect(TreeLinkNode *root) {  
    2: TreeLinkNode* cur = root, *next = NULL;
    3: while(cur!=NULL)
    4: {
    5: TreeLinkNode *p = cur, *k= NULL;
    6: while(p!=NULL)
    7: {
    8: TreeLinkNode* sub = getLinkedLeftNode(p);
    9: if(sub != NULL)
    10: {
    11: if(next == NULL)
    12: {
    13: next = sub;
    14: k = sub;
    15: }
    16: else
    17: k->next = sub;
    18: while(k->next !=NULL) // ietrate to the tail
    19: k = k->next;
    20: }
    21: p = p->next;
    22: }
    23: cur = next;
    24: next = NULL;
    25: }
    26: }
    27: TreeLinkNode* getLinkedLeftNode(TreeLinkNode * root)
    28: {
    29: if(root->left != NULL && root->right != NULL)
    30: root->left->next = root->right;
    31: if(root->left != NULL)
    32: return root->left;
    33: if(root->right != NULL)
    34: return root->right;
    35: return NULL;
    36: }


  • 相关阅读:
    MySQL 性能监控4大指标——第二部分
    mysql 复制A表 到B表;insert into select * from table
    spring boot整合mybatis查询数据库返回Map字段为空不返回解决
    sprinbboot 热部署 造成类加载器 不一致问题
    springboot 整合dubbo 消费模块引入springboot 之后自动注入jdbc 模块导致启动报错问题
    基于Cent os 云服务器中SVN 服务器的搭建---具体实践是可行的 一次备注便于后续查找
    centos7 yum安装配置redis 并设置密码
    ccenteros 部署 redis
    linux ccenteros 部署 redis
    mybatis 插入日期类型精确到秒的有关问题
  • 原文地址:https://www.cnblogs.com/codingtmd/p/5078974.html
Copyright © 2011-2022 走看看