Populating Next Right Pointers in Each Node leetcode java
题目:
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
题解:
这道题解法还是挺直白的,如果当前节点有左孩子,那么左孩子的next就指向右孩子。如果当前节点有右孩子,那么判断,如果当前节点的next是null,
说明当前节点已经到了最右边,那么右孩子也是最右边的,所以右孩子指向null。如果当前节点的next不是null,那么当前节点的右孩子的next就需要
指向当前节点next的左孩子。
递归求解就好。- 代码如下:
- 1 public void connect(TreeLinkNode root) {
- 2 if(root==null)
- 3 return;
- 4 if(root.left!=null)
- 5 root.left.next=root.right;
- 6 if(root.right!=null){
- 7 if(root.next!=null)
- 8 root.right.next = root.next.left;
- 9 else
- root.right.next = null;
- }
- connect(root.left);
- connect(root.right);
- }
Populating Next Right Pointers in Each Node leetcode java的更多相关文章
- leetcode 117 Populating Next Right Pointers in Each Node II ----- java
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Populating Next Right Pointers in Each Node [LeetCode]
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 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 ...
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...
随机推荐
- 什么情况下,会用到fiddler或者charles?
有的页面,比如设限制的html页面,比如原生页面,只能在手机APP里面查看,无法在电脑浏览器中打开查看,这时候,需要用fiddler抓包来查看返回数据,定位问题.
- Hdu5385 The path
The path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- ubuntu下msmtp+mutt的安装和配置
1.mutt+msmtp的安装 默认情况下smokeping发送邮件使用sendmail,但是sendmail配置起来真心不是一般的麻烦,而且也没有必要,完全大材小用了,所以我就想用mutt+msmt ...
- loading加载和layer.js
layer.js中的loading加载 l本篇主要介绍layerjs中的loading加载在实际项目中的应用 1.使用的技术 前端:HTML5+CSS3+JS+layer.js 后端:.net 2.遇 ...
- LINUX 内核 API
http://www.compsoc.man.ac.uk/~moz/kernelnewbies/documents/kdoc/kernel-api/linuxkernelapi.html
- Java深入 - 深入 Java自己定义注解
我们在使用Spring框架的时候,会常常使用类似:@Autowired 这种注解. 我们也能够自定义一些注解.Java的注解主要在包:java.lang.annotation中实现. 1. 元注解 什 ...
- Hadoop: the definitive guide 第三版 拾遗 第四章
第四章中提到了通过CompressionCodec对streams进行压缩和解压缩,并提供了示例程序: 输入:标准输入流 输出:压缩后的标准输出流 // cc StreamCompressor A p ...
- vs2012\vs2013\vs2015碰到生成时报该错误:项目中不存在目标“GatherAllFilesToPublish”
手头一个vs2010升级到vs2012后,web项目发布到本地目录时项目报错:“该项目中不存在目标“GatherAllFilesToPublish”” 通过谷歌大神的帮助,找到了解决方法.共享之. 原 ...
- 浅析iOS程序设计模式(基于MVC)
接触iOS手机开发有一段时间了.总体来说,苹果公司设计的开发环境还是非常人性化的.很容易上手,也方便深入. 在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(ob ...
- ios(包括6、7)应用程序引用系统通讯录的方法 [亲测可行]
由于ios系统对用户隐私的控制,第三方应用程序只能通过苹果官方接口调用系统通讯录,不能像android那样直接操作通讯录数据库. 一般地,使用系统自带通讯录的方法有两种,一种是直接将整个通讯录 ...