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

 Summary: The simplest way is BFS, but we need non-constant extra space. So, I traverses the tree Pre-order, and uses one node pointer for every level.

     void traverse(TreeLinkNode *root, int depth, vector<TreeLinkNode *> &current) {
if(root->left != NULL && root->right != NULL){
if(current.size() < depth + )
current.push_back(root->right);
else{
current[depth]->next = root->left;
current[depth] = root->right;
}
root->left->next = root->right;
//traverse the left subtree
traverse(root->left, depth + , current);
//traverse the right subtree
traverse(root->right, depth + , current);
}
} void connect(TreeLinkNode *root) {
if(root == NULL)
return; vector<TreeLinkNode *> current;
traverse(root, , current);
}

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

  1. Populating Next Right Pointers in Each Node leetcode java

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

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

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

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

  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 每个节点的右向指针

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

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

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

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

  9. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

随机推荐

  1. iOS问题处理:如何在Mac下显示Finder中的所有文件

    摘自:http://www.cnblogs.com/elfsundae/archive/2010/11/30/1892544.html 在Unix下工作,你可能需要处理一些“特殊“文件或文件夹,例如/ ...

  2. 使用@ContextConfiguration注解后,提示找不到配置文件

    intellij提示找不到配置文件 错误代码如下: 严重: Caught exception ] java.lang.IllegalStateException: Failed to load App ...

  3. web服务器安装配置

    学习目标 javaweb概念和web资源分类 服务器的分类和常用服务器apache说明 tomcat 服务器目录结构介绍和工程发布 虚拟主机说明和配置 1.Web的概念 1.1.JavaWeb的概念 ...

  4. 【T-SQL系列】临时表、表变量

    临时表临时表与永久表相似,只是它的创建是在Tempdb中,它只有在一个数据库连接结束后或者由SQL命令DROP掉,才会消失,否则就会一直存在.临时表在创建的时候都会产生SQL Server的系统日志, ...

  5. maven环境快速搭建(转)

    主要介绍maven在本地安装使用 http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html   使用Eclipse构建Maven项目 (s ...

  6. iOS - UIApplication

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder @available(iOS 2.0, *) public ...

  7. js 读写cookie。不同路径会储存各自的cookie。而 在v.net环境下读写是在 / 根目录。

    所以如果全站不分path 的 话.应该显示的写上 path .设置为根目录 function setCookie(name, value) { document.cookie = name + &qu ...

  8. windows 环境变量 %SystemDrive% 和 %SystemRoot%

    %SYSTEMROOT% == c:\windows (or the root windows directory) %SYSTEMDRIVE% == the drive windows is ins ...

  9. Android星星评分控件RatingBar的使用

    在Android的开发中,有一个叫做评分控件RatingBar,我们可以使用该控件做等级划分.评分等作用,星星形状显示,也可以半星级别,我们来看一下评分控件如何使用. 布局文件中定义控件以及属性,这里 ...

  10. 转!!各种数据库的jdbc驱动下载及连接方式

    各种数据库驱动 数据库名称 下载地址 说明 Mysql http://www.mysql.com/products/connector/j/ Shipped. But need to download ...