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. [NOI2017]蔬菜——时光倒流+贪心

    题目链接 题解: 貌似一眼看过去是一个贪心. 其他的算法要记录的东西就太多了. 部分分其实很高.但是没有什么提示. 想一些套路:二分?不行还要贪心判断. 分治?前后取法是有影响的. 时光倒流? 也许可 ...

  2. python基础之魔法方法

    由于hexo自带的markdown渲染引擎对双下划线做了转义,在正文中看到的魔法方法前后都没有双下划线 setattr.getattr.delattr 可以拦截对对象属性的访问 setattr函数是用 ...

  3. EurekaServer集群配置

    一.程序配置 1.pom添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <ar ...

  4. Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机

    Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机: #user nobody; worker_processes 1; #error_log logs/error.log; #err ...

  5. HDU 5636 关键点的 floyd 最短路问题

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  6. 使用feign调用服务的时候注意的问题

    服务端 rest api @RequestMapping(value = "/phone") public ResponsePhone getPhone(@RequestParam ...

  7. ACM1004 Let the balloons fly

    These code is for the problem "Let the balloons Fly" in ACM 1004 which need deal with stri ...

  8. 000 Python常识与快捷键(未完)

    1.Python控制台IDLE的快捷键 Alt + N :返回开始输入的第一条语句 Alt + P :返回刚刚输入的上一条语句 Tab:制表符,用于缩进或补全内容,是Python语法格式的灵魂,作用涵 ...

  9. SqlServer中循环和条件语句示例!

    -- ╔════════╗ -- =============================== ║ if语句使用示例 ║ -- ╚════════╝ declare @a int set @a=12 ...

  10. 2017 济南综合班 Day 2

    木棍(stick) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有很多木棍,具体的,总共有n根,且每根木棍都有一个长度.为了方便起见,我们可以用一个正 ...