LeetCode——Populating Next Right Pointers in Each Node
Description:
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 看到这个题目首先想到的是按层遍历二叉树,然后对每一层做处理,但是仔细读题发现这个做法不好,因为题目要求空间复杂度是O(1):
- 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).
这样可以很容易想到一个非常简单类似前序遍历的方法:
/**
* 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 || root.left == null) {
return ;
} root.left.next = root.right; if(root.next != null) {
root.right.next = root.next.left;
} connect(root.left);
connect(root.right); return;
}
}
虽然上边这种解法是可以AC的,但是也不符合题意,因为这种解法要消耗O(logh)的辅助递归栈空间。
这样的话就要消去递归用循环来写就行了。这样空间复杂度就是O(1),时间复杂度是O(logh);
代码:
/**
* 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 || root.left == null) {
return ;
} TreeLinkNode cur; while(root.left != null) { //深度优先遍历左子树,目的是获取每一层的第一个节点
cur = root;
while(cur != null) { //遍历一层的每一个节点,并用next指针连接
cur.left.next = cur.right;
if(cur.next != null) {
cur.right.next = cur.next.left;
}
cur = cur.next;
}
root = root.left;
} return;
}
}
ZW@QOS_0@NW.png)
这题数据很水,递归也能过。
LeetCode——Populating Next Right Pointers in Each Node的更多相关文章
- 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] 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 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 II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [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 ...
随机推荐
- Intellij IDEA创建Maven Web项目<转>
1前言 在创建项目中,IDEA提供了很多项目模板,比如Spring MVC模板,可以直接创建一个基于Maven的Spring MVC的demo,各种配置都已经设定好了,直接编译部署就可以使用. 最开始 ...
- NIO与传统IO的区别<转>
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- 监控http服务脚本
[root@ob1 scripts]# vim test_httpd.sh 1 #!/bin/bash 2 #ss -tlnup|grep :80 >/dev/null 2>&1 ...
- linu 把文件中的字母小写转换为大写,大写转换为小写awk toupper tolower
cat aa.txt|tr "[a-z]" "A-Z" [root@ob2 mytmp]# awk '{print toupper($0)}' aa2.txt ...
- 修改Linux文件句柄限制
1. 添加ulimit -HSn 655350 到/etc/profile 2. 配置生效 source /etc/profile 修改linux文件句柄数 分类: LINUX 2010-09 ...
- C语言 字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...
- Hibernate的getTransaction()和beginTransaction()
session.getTransaction()只是根据session获得一个Transaction实例,但是并没有启动它 session.beginTransaction()在获得一个Transac ...
- android Menu 笔记
菜单是应用中常见的用户组件.本文介绍如何在布局文件和代码中添加menu,submenu以及在代码中添加的方法. 参考链接 https://developer.android.com/guide/top ...
- Json序列化问题
之前Json字符反序列化为C#对象时 总是写一个实体类.. 如:{"a":5,"b":10} 这种json字符串 对应的实体类为: public class R ...
- php多语言截取字符串函数
<?php header("Content-Type:text/html;charset=utf-8"); function msubstr($str, $start = 0 ...