116 Populating Next Right Pointers in Each Node 每个节点的右向指针
给定一个二叉树
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
填充他的每个 next(下一个)指针,让这个指针指向其下一个右侧节点。如果找不到下一个右节点,则应该将 next(下一个)指针设置为 NULL。
初始状态下,所有 next(下一个)指针 都被设置为 NULL。
注意事项:
您只能使用恒定的额外空间。
你可以假设它是一棵完美二叉树(即所有叶子都在同一水平上,每个父节点有两个孩子)。
例如,鉴于以下完美二叉树,
1
/ \
2 3
/ \ / \
4 5 6 7
调用你的函数后,该树应该变成这样:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
详见:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
Java实现:
递归实现:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if(root==null){
return;
}
if(root.left!=null){
root.left.next=root.right;
}
if(root.right!=null){
root.right.next=root.next!=null?root.next.left:null;
}
if(root.left!=null){
connect(root.left);
}
if(root.right!=null){
connect(root.right);
}
}
}
非递归实现:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if(root==null){
return;
}
LinkedList<TreeLinkNode> que=new LinkedList<TreeLinkNode>();
que.offer(root);
while(!que.isEmpty()){
//记录本层节点的个数
int size=que.size();
for(int i=0;i<size;++i){
TreeLinkNode cur=que.poll();
//最后一个节点的next是null,不做处理
if(i<size-1){
TreeLinkNode next=que.peek();
cur.next=next;
}
if(cur.left!=null){
que.offer(cur.left);
}
if(cur.right!=null){
que.offer(cur.right);
}
}
}
}
}
116 Populating Next Right Pointers in Each Node 每个节点的右向指针的更多相关文章
- [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 ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 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 OJ 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [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 ...
- 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- github如何提交自己修改的代码
当在github上发现别人项目有BUG,或者想要完善其功能的时候,该如何把自己的修改提交到项目中呢? 以logback为例 步骤: 1, fork一份logback代码到自己的仓库 进入github要 ...
- LOJ114 k大异或和
传送门 (vjudge和hdu也有但是我觉得LOJ好看!而且限制少!) 不过本题描述有误,应该是k小. 首先我们需要对线性基进行改造.需要把每一位改造成为,包含最高位的能异或出来的最小的数. 为啥呢? ...
- 实现虚拟机VMware上linux与windows互相复制与粘贴
from:http://blog.csdn.net/u012243115/article/details/40454063 系统环境: win7系统,虚拟机VMwareWorkstation上运行的C ...
- python-pycharm 设置默认代码及注释
pycharm
- 51nod 1327 棋盘游戏——延迟决策的dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1327 因为一列填1个或0个(或0个!!!),而一行不知填多少个,所 ...
- pytest用例setup和teardown
函数式以下两种: setup_function/teardown_function 每个用例开始和结束调用一次 setup_module/teardown_module setup_modu ...
- 修改SQL Server 2005的默认端口
修改SQL Server 2005的默认端口 1.打开SQL Server的配置管理程序 Microsoft SQL Server 2005->配置工具->SQL Server Confi ...
- json 与pickle模块(序列化与反序列化))
一.什么是序列化(pickling): 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化. 序列化可以持久保存状态, 不会根据计算机断电或者重启程序,而使得之前的数据状态丢失.可以在下次程 ...
- 4.1 手写Java PriorityQueue 核心源码 - 原理篇
本章先讲解优先级队列和二叉堆的结构.下一篇代码实现 从一个需求开始 假设有这样一个需求:在一个子线程中,不停的从一个队列中取出一个任务,执行这个任务,直到这个任务处理完毕,再取出下一个任务,再执行. ...
- 535. Encode and Decode TinyURL(rand and srand)
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...