博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
阅读量:5154 次
发布时间:2019-06-13

本文共 3153 字,大约阅读时间需要 10 分钟。

Populating Next Right Pointers in Each Node

OJ:

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) {        TreeLinkNode *p = root;        while(p && p->left) {            p->left->next = p->right;            TreeLinkNode *pre = p, *cur = p->next;            while(cur) {                pre->right->next = cur->left;                cur->left->next = cur->right;                pre = cur;                cur = cur->next;            }            p = p->left;        }    }};

 

Populating Next Right Pointers in Each Node II

 OJ:

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) {        TreeLinkNode *curListNode, *startNode, *curNode;        startNode = root;        while(startNode) {            curNode = startNode;            startNode = curListNode = NULL;            while(curNode) {                if(curNode->left) {                    if(startNode == NULL) startNode = curNode->left;                    if(curListNode == NULL) curListNode = curNode->left;                    else { curListNode->next = curNode->left; curListNode = curListNode->next; }                }                if(curNode->right) {                    if(startNode == NULL) startNode =curNode->right;                    if(curListNode == NULL) curListNode = curNode->right;                    else { curListNode->next = curNode->right; curListNode = curListNode->next; }                }                curNode = curNode->next;            }        }    }};

 

转载于:https://www.cnblogs.com/liyangguang1988/p/3939512.html

你可能感兴趣的文章
List_统计输入数值的各种值
查看>>
学习笔记-KMP算法
查看>>
Timer-triggered memory-to-memory DMA transfer demonstrator
查看>>
跨域问题整理
查看>>
[Linux]文件浏览
查看>>
64位主机64位oracle下装32位客户端ODAC(NFPACS版)
查看>>
获取国内随机IP的函数
查看>>
今天第一次写博客
查看>>
江城子·己亥年戊辰月丁丑日话凄凉
查看>>
IP V4 和 IP V6 初识
查看>>
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
C语言基础小结(一)
查看>>
STL中的优先级队列priority_queue
查看>>
UE4 使用UGM制作血条
查看>>
浏览器对属性兼容性支持力度查询网址
查看>>
OO学习总结与体会
查看>>
虚拟机长时间不关造成的问题
查看>>