问题

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

Initially, all next pointers are set toNULL.

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

思路

首先判断根节点是否为null,如果是,return;如果不是,继续。

设置节点node,用来指向每层的第一个节点;设置节点next,用来指向该层的每个节点。

当node的左孩子存在时,使用next来遍历该层的其它节点:

  (1)让node左孩子的next指向node右孩子;

  (2)若next.next不为null,让node右孩子的next指向next.next的左孩子。

代码

 /**
* 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;
root.next = null;
TreeLinkNode node = root, next = node;
while(node.left!=null){
next = node;
while(next!=null){
next.left.next = next.right;
if(next.next!=null)
next.right.next = next.next.left;
next = next.next;
}
node = node.left;
}
}
}

树——populating-next-right-pointers-in-each-node(填充每个节点的next指针)的更多相关文章

  1. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

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

  2. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

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

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

  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. Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  6. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

  7. 117 Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    这是“每个节点的右向指针”问题的进阶.如果给定的树可以是任何二叉树,该怎么办?你以前的解决方案仍然有效吗?注意:    你只能使用恒定的空间.例如,给定以下二叉树,         1       / ...

  8. Populating Next Right Pointers in Each Node I, II——生成next树

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

  9. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

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

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

随机推荐

  1. #1114-JSP指令

    JSP 指令 JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言. 语法格式如下: <%@ directive attribute = "value"%& ...

  2. Spring、Hibernate、Struts官方下载地址

    hibernate 官网: http://hibernate.org/ hibernate3 官方下载:http://sourceforge.net/projects/hibernate/files/ ...

  3. 关于 Visual stdio 编译报错:error MSB6006: “CL.exe”已退出

    网上查看,原因有多种. 1,我自己遇到的是这样的: 环境:VS2019,编译项目 image-master,中间自己重整了原来的目录,移动了很多文件.编译报错:error MSB6006: “CL.e ...

  4. 【后台管理系统】—— Ant Design Pro组件使用(一)

    一.搜索Search      搜索框 <Search placeholder="请输入关键字" defaultValue={kw && kw != 'nul ...

  5. 单页应用 cookies处理

    w Node & 单页应用 来做一个完整用户系统吧! - harryfyodor的前端专栏 - SegmentFaulthttps://segmentfault.com/a/119000000 ...

  6. C# winform OpenFileDialog用法

    https://jingyan.baidu.com/article/e52e36156fa6d240c60c51c8.html 详情看看这个.

  7. 07 oracle 归档模式 inactive/current redo log损坏修复--以及错误ORA-00600: internal error code, arguments: [2663], [0], [9710724], [0], [9711142], [], [], [], [], [], [], []

    07 oracle 归档模式 inactive/current redo log损坏修复--以及错误ORA-00600: internal error code, arguments: [2663], ...

  8. 你还没有真正理解的innodb_flush_log_at_trx_commit

    关于innodb_flush_log_at_trx_commit的描述,看了mysql手册中的解释,感觉都不够清晰明了,下面试图以最简单直白的方式解释一下innodb_flush_log_at_trx ...

  9. arduino库函数1

    https://wenku.baidu.com/view/e657b1f0bcd126fff6050baf.html 的阅读笔记.现在到了 第四十页. setup应该是 在开始 执行一次. 然后 lo ...

  10. Mysql 免安装版本配置

    1. 安装命令 (制定安装目录的my.ini文件) mysqld --install MySQL --defaults-file="C:\mysql-5.7.26-winx64\bin\my ...