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
/*** 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) {}* };*/
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 *head = NULL;TreeLinkNode *prev = NULL;TreeLinkNode *cur  = root;while(cur != NULL){while(cur != NULL){if(cur->left != NULL){if(prev != NULL){prev->next = cur->left;}else{head = cur->left;}prev = cur->left;}if(cur->right != NULL){if(prev != NULL){prev->next = cur->right;}else{head = cur->right;}prev = cur->right;}cur = cur->next;}cur = head;head = NULL;prev = NULL;}}
};

转载于:https://www.cnblogs.com/yxwkf/p/5079905.html

leetcode - Populating Next Right Pointers in Each Node II相关推荐

  1. 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 ...

  2. LeetCode Populating Next Right Pointers in Each Node II(dfs)

    问题:给出一个二叉查找树,将结点与其右边的结点相连 思路: 从顶向上,从右向左的方式 .递归过程中,在当前结点及父结点作为参数传递.在向下的过程中,如果父结点不为空则获取当前结点的next结点.如果当 ...

  3. 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II

    LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...

  4. 117 Populating Next Right Pointers in Each Node II

    117 Populating Next Right Pointers in Each Node II 就是 Bibary Tree Level order Traverse class Solutio ...

  5. LeetCode 117. Populating Next Right Pointers in Each Node II

    原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...

  6. LeetCode OJ - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  7. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  8. Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  9. 117. Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

最新文章

  1. git 设置有效目录_如何有效使用Git
  2. C++自定义高斯滤波器
  3. PHP file_get_contents 设置超时时间
  4. linux rm 命令删除文件恢复_rm删除文件空间就释放了吗?天真!
  5. python不会英语不会数学怎么自学-26岁了,自学Python怎么样?
  6. Linux rm , cp, mv 命令
  7. cmd bat 相对命令
  8. 77个互联网+大学生创新创业大赛文档
  9. 国家信息安全水平考试NISP一级官方视频知识点整理
  10. 国内最强Web3D软件WebMax正式版发布
  11. SQLmap简单的使用——新手教程解析
  12. 五十三、微信小程序云开发豆瓣电影小项目
  13. MAC——本机域名[localhost]配置
  14. 转:如何做好团队复盘?3大阶段、9个步骤
  15. 美式英语口语中连读、略读,音变的技巧
  16. 家庭智能控制系统开发
  17. 89.破碎的玻璃横幅
  18. [Zookeeper] 安装and各 配置参数详解
  19. Error occurred during initialization of VM 附oracle官网解决方式
  20. win10重新安装ie

热门文章

  1. VGA、DVI、HDMI三种视频信号接口差别
  2. 2000坐标系高程与85高程转换_科普 | 如何在大疆智图中设置坐标系
  3. centos7编译安装mysql5.7_在Centos7源码包编译安装MySQL5.7
  4. 用计算机计算器课程,信息技术学科“神奇的计算器”教学设计
  5. python具有一些突出优点_Python具有一些突出优点,它们是:()
  6. 不干胶标贴打印模板下载_A4纸打印17*6的不干胶标签打印模板如何设置
  7. 重庆理工大学计算机考研就业,重庆理工大学专业硕士含金量高吗?好就业吗?
  8. 052_Function对象
  9. Java高并发编程:多个线程之间共享数据的方式探讨
  10. Android安全加密:非对称加密