LeetCode: Populating Next Right Pointer in Each Node

Given a binary tree

  1. struct TreeLinkNode {
  2. TreeLinkNode *left;
  3. TreeLinkNode *right;
  4. TreeLinkNode *next;
  5. }

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. 1
  2. / \
  3. 2 3
  4. / \ / \
  5. 4 5 6 7

After calling your function, the tree should look like:

  1. 1 -> NULL
  2. / \
  3. 2 -> 3 -> NULL
  4. / \ / \
  5. 4->5->6->7 -> NULL

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/

算法:用递归好简单有木有。按上面的例子,先完成左右子树的连接,然后,根的next指向空,第二个节点指向第三个节点,第五个节点指向第六个节点。代码:

  1. /**
  2. * Definition for binary tree with next pointer.
  3. * struct TreeLinkNode {
  4. * int val;
  5. * TreeLinkNode *left, *right, *next;
  6. * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. void connect(TreeLinkNode *root) {
  12. if(!root) return ;
  13. root->next = NULL;
  14. connect(root->left);
  15. connect(root->right);
  16. TreeLinkNode *p = root->left;
  17. TreeLinkNode *q = root->right;
  18. while(p && q){
  19. p->next = q;
  20. p = p->right;
  21. q = q->left;
  22. }
  23. }
  24. };

第二题:

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. 1
  2. / \
  3. 2 3
  4. / \ \
  5. 4 5 7

After calling your function, the tree should look like:

  1. 1 -> NULL
  2. / \
  3. 2 -> 3 -> NULL
  4. / \ \
  5. 4-> 5 -> 7 -> NULL

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

算法:这道题与前面一题不同的是,这里的二叉树不再是完美二叉树,而是一颗普通的二叉树。由于是一颗普通的二叉树,所以沿着右指针走不一定会到达下一层的最后一个节点,因为这个右指针可能为空;同样,沿着左指针走也不一定会到达下一层的第一个节点,因为这个左指针可能为空。所以,我们需要一个找到当前节点下一层的第一个节点的函数,然后顺着next也可以到达该层的最后一个节点。利用这样的函数,我们就可以完成题目的要求。代码:

  1. /**
  2. * Definition for binary tree with next pointer.
  3. * struct TreeLinkNode {
  4. * int val;
  5. * TreeLinkNode *left, *right, *next;
  6. * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. void connect(TreeLinkNode *root) {
  12. if(!root) return ;
  13. root->next = NULL;
  14. connect(root->left);
  15. connect(root->right);
  16. TreeLinkNode *p = root->left;
  17. TreeLinkNode *q = root->right;
  18. while(p && q){
  19. TreeLinkNode *r = nextLevelFirst(p);
  20. while(p->next){
  21. p = p->next;
  22. }
  23. p->next = q;
  24. p = r;
  25. q = nextLevelFirst(q);
  26. }
  27. }
  28. TreeLinkNode * nextLevelFirst(TreeLinkNode *p){
  29. while(p){
  30. if(p->left){
  31. return p->left;
  32. }else if(p->right){
  33. return p->right;
  34. }
  35. p = p->next;
  36. }
  37. return NULL;
  38. }
  39. };

LeetCode: Populating Next Right Pointer in Each Node的更多相关文章

  1. [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

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

  2. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

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

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

  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 II 解题报告

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

  7. LEETCODE —— Populating Next Right Pointers in Each Node

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

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

  9. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

随机推荐

  1. 获取json中字段,判断是否有想要的key

    if(json.containsKey("key")){ String refundSid = json.getString("key"); } 如果也要判断v ...

  2. DateTime日期计算

    //今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-1).ToShortDateStrin ...

  3. redis集群的搭建

    1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...

  4. web服务器分析与设计(二)

    面向对象分析与设计第二步:寻找对象,建立问题域模型 1,用例场景描述 接上一篇中的用例,编写用例场景 U1: 上网者:打开网站(www.xxx.com) 浏览器:连接网站 目标系统:接受连接 检查连接 ...

  5. CSS 高级:尺寸、分类、伪类、伪元素

    CSS 尺寸:允许你控制元素的高度和宽度.同样,还允许你增加行间距. CSS 分类:允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的 ...

  6. Windows服务-手把手带你体验

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...

  7. Things App Engine Doesn't Do...Yet

    当人们第一次使用App Engine的时候,他们会问一些App Engine不会做的事情.其中的一些事情Google在不久的将来会实现的,还有一些违背了App Engine设计的本质,将不可能增加(到 ...

  8. 关于P2P架构的网络游戏

    以下内容摘自<ActionScript大型网页游戏开发> ————————————————————————————————————————————————————————— P2P架构 P ...

  9. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  10. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...