Populating Next Right Pointers in Each Node

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
 
 
 
连接时,按照下面的规则进行就可以了:
left->next=father->right
right->next=father->next->left;
 
 /**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) { if(root==NULL) return; if(root->left!=NULL) root->left->next=root->right;
if(root->right!=NULL)
{
if(root->next!=NULL) root->right->next=root->next->left;
}
connect(root->left);
connect(root->right); }
};

【leetcode】Populating Next Right Pointers in Each Node的更多相关文章

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

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

  2. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  3. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

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

  4. 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node

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

  5. 【Leetcode】【Medium】Populating Next Right Pointers in Each Node

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

  6. 【树】Populating Next Right Pointers in Each Node

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

  7. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

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

随机推荐

  1. HDInsight 指定输出目录 insert overwrite

    基本语法 insert overwrite local directory '/example/demo/' select * from table; 可以格式化输出 insert overwrite ...

  2. AndroidManiFast 字段意义

    每个Activity都要在本文件中注册. <Activity>下的<Intent-filter>中. 两个字段的意思是: <action android:name=&qu ...

  3. knockout-validation不自动插入错误消息

    <div data-bind="validationOptions:{insertMessages:false}"> <div class="valid ...

  4. Linux查看软件安装路径

    Linux中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. 这里给大 ...

  5. javascript 时间处理

    <script language="JavaScript" type="text/JavaScript"> var myDate = new Dat ...

  6. SQL Server 2012不支持从SQL Server 2000的备份进行还原

    错误: dbbackup failed: Unable to restore database 'ppt'Not valid backupThe database was backed up on a ...

  7. Spark之Streaming

    1. socket消息发送 import java.net.ServerSocket import java.io.PrintWriter import scala.collection.mutabl ...

  8. Go - 内置函数大全

    Package builtin import "builtin" Overview Index Overview ▾ Package builtin provides docume ...

  9. C语言中的#define预处理指令

    本文链接:http://www.cnblogs.com/xxNote/p/4009460.html 今天看C Primer Plus里面看449页里面 16.2.1语言符号 讲到从技术方面看,系统把宏 ...

  10. /etc/bashrc和/etc/profile傻傻分不清楚?

    导读 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文 ...