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;
}
}

这题数据很水,递归也能过。

LeetCode——Populating Next Right Pointers in Each Node的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  4. 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 ...

  5. [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 ...

  6. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  7. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  8. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  9. LeetCode: Populating Next Right Pointers in Each Node 解题报告

    Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode {      Tree ...

  10. [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 ...

随机推荐

  1. 机器学习:Mean Shift聚类算法

    本文由ChardLau原创,转载请添加原文链接https://www.chardlau.com/mean-shift/ 今天的文章介绍如何利用Mean Shift算法的基本形式对数据进行聚类操作.而有 ...

  2. TCP协议的问题

    Server端接收到Client端信息后不会返回给Client端 // TCPEchoServer.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h&quo ...

  3. 页面装载js及性能分析方法

    一.装载 先装载静态页面的引用js文件,然后查找引用文件中是否包含onload函数,比如main.js中包含onload函数,在main.js中查找是否有对其他js文件的引用,优先装载引用js文件,被 ...

  4. [svn] 在线安装

    SVN地址:http://subclipse.tigris.org/update_1.6.x/

  5. 德国Aptamil不同系列奶粉间差别

    以下内容均来源网络整理.汇总. 德国人做事严谨,而且对于有争议性的成分持保守态度,比如不添加麦芽糊精.所以我比较赞赏购买德国的奶粉,主要是aptamil和hipp喜宝,这两个牌子也基本没有负面新闻.但 ...

  6. thinkphp 操作mssql2008

    配置文件 <?php return array( //'配置项'=>'配置值' //'USERNAME'=>'admin', //赋值 //数据库配置信息 'DB_TYPE' =&g ...

  7. apache -- 端口被占用

    需求不断变更,总会安装不同的软件,这些软件理想情况下会使用不同的端口,但还是有软件之间端口被占的情况. 最近在电脑上装了一个VMware虚拟机软件,结果导致开启本机的Apache服务无法启动,报 “T ...

  8. 学习:List的扁平化 和 拼接

    一.list_to_binary/1的参数:iolist类型的. 二.lists:concat(Things) -> string() Types: Things = [Thing] Thing ...

  9. jquery validate自定义规则

    //检查身份证号码是否存在 $.validator.addMethod("checkIDCardExist", function (value, element) { if ($( ...

  10. Tomcat 配置 项目 到tomcat目录外面 和 域名绑定访问(api接口、前端网站、后台管理网站)

    先停止tomcat服务 1.进入apache-tomcat-7.0.68/conf/Catalina/localhost(如果之前还都没有启动过tomcat,是不会有此目录的,先启动一次再关闭,会自动 ...