题目地址: https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

关键思路:讲节点的左右子节点链接起来,遍历节点的next节点,即可。

class Solution {
public:
TreeLinkNode* gethead(TreeLinkNode* root)
{
if(root == NULL)
return NULL;
if(root->left != NULL)
return root->left;
else
return root->right;
} TreeLinkNode* gettail(TreeLinkNode* root)
{
if(root == NULL)
return NULL;
if(root->right != NULL)
return root->right;
else
return root->left;
} void connect(TreeLinkNode *root)
{
TreeLinkNode* head = NULL;
TreeLinkNode* tail = NULL;
if(root == NULL)
return; TreeLinkNode* pcur = root;
while(root != NULL)
{
TreeLinkNode* headtmp = gethead(root);
TreeLinkNode* tailtmp = gettail(root); if(headtmp == NULL)
{
root= root->next;
continue;
} if(headtmp != tailtmp)
headtmp->next = tailtmp; if(tail != NULL)
{
tail->next = headtmp;
tail = tailtmp;
}
else
{
head = headtmp;
tail = tailtmp;
}
root = root->next;
} while(pcur != NULL)
{
TreeLinkNode* tmp = gethead(pcur);
if(tmp != NULL)
{
connect(tmp);
break;
}
pcur = pcur->next;
}
} };

Populating Next Right Pointers in Each Node II的更多相关文章

  1. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  2. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  3. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  4. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

  5. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  6. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  7. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

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

  8. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  10. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

随机推荐

  1. Android实现数据存储技术

    转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...

  2. Linux下的线程

    一.线程的优点 与传统进程相比,用线程来实现相同的功能有如下优点: (1)系统资源消耗低. (2)速度快. (3)线程间的数据共享比进程间容易的多. 二.多线程编程简单实例 #include < ...

  3. Bash脚本编程基础

    为实现某个任务,将许多命令组合后,写入一个可执行的文本文件的方法,称为Shell脚本编程. 按照应用的Shell环境不同,可以将Shell脚本分为多种类型.其中最常见的是应用于Bash和Tcsh的脚本 ...

  4. 检测MYSQL不同步发邮件通知的脚本

    脚本代码如下:#!/bin/bash                                                                                   ...

  5. TCP/IP协议原理与应用笔记21:路由选择的方法

    1. 路由选择的方法 (1)基本思想 不是收到IP分组后才能为其选路,而是预先获得所有的目的的路由(Routing Protocol) IP报文按预定的路由转发(route table) (2)预定路 ...

  6. Android进阶笔记19:onInterceptTouchEvent、onTouchEvent与onTouch

    1.onTouch方法:onTouch方法是View的 OnTouchListener借口中定义的方法,处理View及其子类被touch是的事件处理.当一个View绑定了OnTouchLister后, ...

  7. (一)u-boot2013.01.01 for TQ210:《Uboot简介》

    一直想写一个s5pv210硬件平台的u-boot的移植文档,但一直都忙着没时间写.先写一些u-boot的脚本分析吧,包括makefile,mkconfig,config.mk,主要侧重于语法句意的分析 ...

  8. DML 数据操控语言

    一.DML数据操作语言  (DQL  select ) 主要用于检索.插入和修改数据库信息.它是最常用的SQL命令,如INSERT(插入).UPDATE(更新).SELECT(选择).DELETE(删 ...

  9. CF Preparing Olympiad (DFS)

    Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. hdu 4714 树形DP

    思路:dp[i][0]表示第i个节点为根的子树变成以i为一头的长链最小的花费,dp[i][0]表示表示第i个节点为根的子树变成i不是头的长链最小花费. 那么动态方程也就不难想了,就是要分几个情况处理, ...