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. Java_类文件及加载机制

    类文件及类加载机制 标签(空格分隔): Java 本篇博客的重点是分析JVM是如何进行类的加载的,但同时我们会捎带着说一下Class类文件结构,以便对类加载机制有更深的理解. 类文件结构 平台无关性 ...

  2. Node.js 事件循环(Event Loop)介绍

    Node.js 事件循环(Event Loop)介绍 JavaScript是一种单线程运行但又绝不会阻塞的语言,其实现非阻塞的关键是“事件循环”和“回调机制”.Node.js在JavaScript的基 ...

  3. emacs学习

    (set-default-font "Consolas-14")      // 设置字体及其大小 M-数字 命令 C-数字 命令

  4. 使用escape编码地址栏中的中文字符

    在通过地址栏传递参数的时候,有时候会遇到中文参数,在获取这种中文参数值得时候, 往往会出现乱码, 解决办法如下: 在传递参数的使用 escape 函数进行编码,获取的时候再进行解码即可. 例如: va ...

  5. Xstream 学习地址

    http://forestqqqq.iteye.com/category/301129

  6. MVC3远程验证

    public class StudentModel { [Display(Name="学生编号")] public int StuId { set; get; } [Require ...

  7. poj3855Blast the Enemy!(多边形重心)

    链接 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  8. mysql delimiter

    默认情况下,mysql遇到分号; 就认为是一个命令的终止符, 就会执行命令.而有些时候,我们不希望这样,比如存储过程中包含多个语句,这些语句以分号分割,我们希望这些语句作为一个命令,一起执行,怎么解决 ...

  9. gcc, numpy, rabbitmq等安装升级总结

    1. 公司在下面目录安装了gcc-4.8.2,以支持c++11,可以通过在bashrc中添加来实现: PATH=/opt/compiler/gcc-4.8.2/bin:$PATH 2. 公司环境切换到 ...

  10. Android Fragment分页显示的实现

    分页显示有两种方式 一种是使用ViewPager 另一种是使用FragmentTransaction 上代码 1 FragmentTransaction实现方式 public class MainAc ...