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 ...
随机推荐
- PHP菜刀工具WebHandler
PHP菜刀工具WebHandler 在Web渗透测试中,后台代码如果包含系统命令执行功能,并以用户提交的数据作为参数,就带来潜在的安全隐患.Kali Linux提供一款PHP菜单工具WebHand ...
- NOIp模拟赛 巨神兵(状压DP 容斥)
\(Description\) 给定\(n\)个点\(m\)条边的有向图,求有多少个边集的子集,构成的图没有环. \(n\leq17\). \(Solution\) 问题也等价于,用不同的边集构造DA ...
- JSONPATH使用方法
如下的json: { "store": { "book": [ { "category": "reference", & ...
- C# 对WinForm应用程序的App.config的使用及加密
原文地址:http://blog.163.com/zhou_zzq/blog/static/1019622120137621739874/ 我们在写C#应用程序时,在工程文件中放置一个app.co ...
- SQL 查询逻辑处理顺序
http://www.cnblogs.com/lyhabc/articles/3912608.html http://blog.csdn.net/lanxu_yy/article/details/62 ...
- iOS十进制切割格式转换
//@"123456789" 转换后 @"123,456,789" @interface NSString (num) - (NSString *)money; ...
- 【leetcode】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
- ASP.NET Web API中展示实体Link相关的方面
有时候,向服务端请求一个实体,我们希望返回如下的格式: links: [ href: http://localhost:8901/api/user/diaries/2013-08-17, ...
- Delphi的自动编译软件Want
Delphi自动编译环境的搭建及使用 什么是Want Want是一套Windows下用于编译Delphi源代码的工具.Want的名称是Windows Ant的意思. Ant是Java下最著名的自动编译 ...
- Javascript数据模型
本文主要描述Javascript的数据模型,即对Javascript所支持的数据类型的一个全局概缆.文章比较的理论化,非常深入,因此不易理解,但务必对数据模型有一个映象,因为他是理解Javascrip ...