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. Logical Address->Linear Address->Physical Address

    3 registers for starting pos: LDTR, GDTR( register for starting addr of DT) ---段描述符每个段由一个8字节(64位)的段描 ...

  2. 关于float的说明

    关于float的说明     如图所示,蓝色的div和红色的div处于黑色的div(宽为500px)之中,其中蓝色的div设置了做浮动: 由图一(红色div的margin-top为10px,margi ...

  3. CSS3选择器 :nth-child(n) 详解

    CSS3 :nth-child(n): http://demo.doyoe.com/css3/nth-child(n)/ 浏览器参照基准:IE9, Firefox, Chrome, Safari, O ...

  4. Linux常见问题的处理方法(长期更新)

    一.使用sudo命令时xxx is not in the sudoers file. This incident will be reported. 1.su -,输入root的密码完成身份切换. 2 ...

  5. 通过NuGet获取sqlite对应的.net的dll

    https://www.nuget.org/packages/System.Data.SQLite/ 直接在Package Manager Console中执行命令,会自动安装依赖项的 Install ...

  6. MeshLab中进行点云配准

    MeshLab是一个开源.可移植和可扩展的三维几何处理系统,主要用于交互处理和非结构化编辑三维三角形网格.它支持多种文件格式: import:PLY, STL, OFF, OBJ, 3DS, COLL ...

  7. Using Post_Query Trigger in Oracle Forms

    When a query is open in the block, the Post-Query trigger fires each time Form Builder fetches a rec ...

  8. 第一次写python爬虫

    花了4天终于把写完了把国内的几个漏洞平台爬完了,第一次写py,之前一直都在说学习,然后这周任务是把国内的漏洞信息爬取一下.花了1天学PY,剩下的1天一个.期间学习到了很多.总结如下: ======== ...

  9. DIV与IDIV的用法

    DIV (unsigned divide) 无符号数除法 格式:DIV SRC 执行的操作: 字节操作:16位被除数在AX,8位除数为源操作数,结果的8位商在AL中,8位余数在AH中.表示为 (AL) ...

  10. style不同取值对应的日期、时间格式

    from : http://www.cnblogs.com/Gavinzhao/archive/2009/11/10/1599690.html sql server2000中使用convert来取得d ...