Populating Next Right Pointers in Each Node I

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

乍一看很难,理清思路后很简单的。

/**
* 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||root->left==NULL)
return;
root->left->next=root->right;
if(root->next!=NULL)
root->right->next=root->next->left;
connect(root->left);
connect(root->right);
return ;
}
};

 

II

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

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
/ \
2 3
/ \ \
4 5 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
/**
* 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(NULL == root) return;
TreeLinkNode* start;
TreeLinkNode* curNode;
TreeLinkNode* nextNode;
while(root != NULL){
start = findStartNodeNextLev(root);
curNode = start;
nextNode = findNextNodeNextLev(root, start);
while(nextNode != NULL){
curNode -> next = nextNode;
curNode = nextNode;
nextNode = findNextNodeNextLev(root, curNode);
}
root = start;
}
}
private:
TreeLinkNode* findNextNodeNextLev(TreeLinkNode* &cur, TreeLinkNode* curNextLev){
if(cur -> left == curNextLev && cur -> right != NULL){
return cur -> right;
}else{
while(cur -> next != NULL){
cur = cur -> next;
if(cur -> left != NULL && cur -> left != curNextLev) return cur -> left;
if(cur -> right != NULL && cur -> right != curNextLev) return cur -> right;
}
}
return NULL;
} TreeLinkNode* findStartNodeNextLev(TreeLinkNode* node){
if(NULL == node) return NULL;
if(node -> left != NULL) return node -> left;
return findNextNodeNextLev(node, node -> left);
}
};

  

Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看的更多相关文章

  1. Populating Next Right Pointers in Each Node(I and II)

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

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

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

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

  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

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  7. LeetCode - Populating Next Right Pointers in Each Node II

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

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

  9. 【leetcode】Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

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

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

随机推荐

  1. nginx基于域名的虚拟主机配置(本地分布式项目域名配置及测试方法)

    最有用的虚拟主机配置方式. 一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定. 可以修改host文件实现域名访问. 前提:即使我们在nginx中配置基于域名的虚拟主机,也需要域名解析,即n ...

  2. VC对话框实现添加滚动条实现滚动效果

    对话框滚动条及滚动效果实现,用的api主要有: ScrollWindow, SetScrollInfo, GetScrollInfo, SetWindowOrgEx.涉及的数据结构为SCROLLINF ...

  3. HDU1569 最大流(最大点权独立集)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  4. each jquery

    <div class="first"> <span>投保人数:</span> <input type="text" i ...

  5. Android 一些系统参数的获取

    //获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...

  6. c# txt 文件上传、写入TXT文件、创建图形验证码

    asp.net mvc 图片上传 html 在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性 <form encty ...

  7. table第一行合并,其余行宽度失效问题

    <col>标签 http://www.w3school.com.cn/tags/tag_col.asp 由于表格中有一行合并过,所以其它没有合并的行的列宽就会平均化,对列宽的设置也会失效. ...

  8. nodejs与mongo

    1.连接URL (使用数据用户名与密码连接或不使用连接数据库) npm install mongodb --save var mon = require('mongodb').MongoClient; ...

  9. Stirling数笔记

    Updating.... 这几个玩意儿要记的东西太多太乱所以写blog整理一下 虽然蒯的成分会比较多全部 我居然开始记得写blog了?? 第一类 这里讨论的是无符号类型的. OEIS编号A130534 ...

  10. Sass 基本函数

    Sass 中的常用函数 一.字符串函数 1. unquote($string): 删除字符串前后的引号,删除一对引号,如果这个字符串没有带有引号,将返回原始的字符串. 示例: .text1 { con ...