Given a binary tree

  1. struct TreeLinkNode {
  2. TreeLinkNode *left;
  3. TreeLinkNode *right;
  4. TreeLinkNode *next;
  5. }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

  1. 1
  2. / \
  3. 2 3
  4. / \ / \
  5. 4 5 6 7

After calling your function, the tree should look like:

  1. 1 -> NULL
  2. / \
  3. 2 -> 3 -> NULL
  4. / \ / \
  5. 4->5->6->7 -> NULL

思路:一般层次遍历需要申请队列,但是有了next指针,我们只需记录每层最左的节点,所以可以做到use constant extra space

  1. class Solution {
  2. public:
  3. void connect (TreeLinkNode *root){
  4. TreeLinkNode* parent;
  5. TreeLinkNode* current;
  6. TreeLinkNode* nextParent = root;
  7. while(nextParent){ //new level started
  8. parent = nextParent;
  9. nextParent = parent->left;
  10. current = nextParent;
  11. if(!current) return;
  12.  
  13. //add next pointer
  14. current->next = parent->right;
  15. current = current->next;
  16. if(!current) return;
  17. while(parent->next){
  18. parent = parent->next;
  19. current->next = parent->left;
  20. current = current->next;
  21. if(!current) return;
  22. current->next = parent->right;
  23. current = current->next;
  24. if(!current) return;
  25. }
  26. }
  27. }
  28. };

116. Populating Next Right Pointers in Each Node (Tree; WFS)的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  2. [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. LeetCode OJ 116. Populating Next Right Pointers in Each Node

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  6. 116. Populating Next Right Pointers in Each Node

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  7. leetcode 116 Populating Next Right Pointers in Each Node ----- java

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

随机推荐

  1. Java中的HashMap的工作原理是什么?

    问答题23 /120 Java中的HashMap的工作原理是什么? 参考答案 Java中的HashMap是以键值对(key-value)的形式存储元素的.HashMap需要一个hash函数,它使用ha ...

  2. 为什么要使用索引?-Innodb与Myisam引擎的区别与应用场景

    Innodb与Myisam引擎的区别与应用场景 http://www.cnblogs.com/changna1314/p/6878900.html https://www.cnblogs.com/ho ...

  3. 《DSP using MATLAB》示例 Example 10.2

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. 使用mongoperf评估磁盘随机IO性能

    用法举例: # 16个io线程 # 随机读写10GB的测试文件 echo "{nThreads:16,fileSizeMB:10000,r:true,w:true}" | mong ...

  5. git源站安装

    ##下载源站 wget https://www.kernel.org/pub/software/scm/git/git-2.15.0.tar.xz ##安装依赖组件 yum install curl- ...

  6. Mysql的“Table 'mysql.servers' doesn't exist”的解决方法

    安装MYSQL后,又一次系统出现问题了,于是我查看mysql的错误日志,竟发现Table 'mysql.servers' doesn't exist问题的错误, 虽然与我的问题无关,但这个问题还是引起 ...

  7. WebKit的已实施srcset图像响应属性

    WebKit已经发布了一些官方新闻,终于落实srcset的属性.作为W3C的响应图像社区组的主席,我一直希望这一刻到来有一段时间了.所以,对所有参与方是个好消息,用户浏览网页时的体验是最重要的. 所有 ...

  8. Log4j(3)--rootLogger根配置和appender输出类型配置

    参考博客:http://blog.java1234.com/blog/articles/270.html 一.rootLogger根配置: Log4j 根配置语法 log4j.rootLogger = ...

  9. Chrome JS断点调试技巧

    Chrome调试折腾记_(2)JS断点调试技巧 技巧一:格式化压缩代码 技巧二:快速跳转到某个断点的位置 技巧三:查看断点内部的作用范围[很实用] 技巧4:监听事件断点 技巧5:DOM及 XHR监听跳 ...

  10. mysql 使用inet_aton和inet_ntoa处理ip地址数据

    mysql 使用inet_aton和inet_ntoa处理ip地址数据 mysql提供了两个方法来处理ip地址 inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转 ...