题目:

Given a binary tree

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

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
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

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

思路:

递归,然后把中间的节点给连起来

package tree;

class TreeLinkNode {
int val;
TreeLinkNode left, right, next;
TreeLinkNode(int x) { val = x; }
} public class PopulatingNextRightPointersInEachNode { public void connect(TreeLinkNode root) {
if (root == null) return;
connect(root.left);
connect(root.right);
TreeLinkNode left = root.left;
TreeLinkNode right = root.right;
while (left != null && right != null) {
left.next = right;
left = left.right;
right = right.left;
}
} }

最新文章

  1. QT5.1 调用https
  2. 支付宝alipay使用小结 调用支付宝程序被杀死说明
  3. C#动态编译代码,执行一个代码片段,或者从指定文件中加载某个接口的实现类
  4. Debian下安装vim
  5. STL中的算法小结
  6. ecshop添加商品选择品牌时如何按拼音排序
  7. java语言基础02
  8. nginx1.8+php5.6.10 服务器编译安装备忘2015-06
  9. 基于Networks of Brokers的HA方案
  10. CodeForces 379 D. New Year Letter
  11. JAVA 命令参数详解System.setProperty(
  12. Python 用IMAP接收邮件
  13. iOS 之 导航栏按钮
  14. 将 MacOS 默认的 PHP 版本升级到 7.*
  15. 使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) smtp connect() failed;
  16. springMVC源码分析--RequestMappingHandlerAdapter(五)
  17. HBuilderx中编译sass文件
  18. <算法图解>读书笔记:第1章 算法简介
  19. HDU - 3642 Get The Treasury(线段树求体积交)
  20. linux使用curl上传文件并且同时携带其它传递参数

热门文章

  1. 详解SQL集合运算
  2. JavaScript使用DeviceOne开发实战(一) 配置和起步
  3. 今天心情好,给各位免费呈上200兆SVN代码服务器一枚,不谢!
  4. [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决
  5. JavaScript正则表达式下——相关方法
  6. AngularJS入门教程1--配置环境
  7. Git学习笔记(3)——撤销修改和文件的删除
  8. 安装Jenkins
  9. jQuery UI AutoComplete的使用
  10. MyEclipse使用总结——MyEclipse10安装SVN插件