Boostable

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  116 随笔 :: 0 文章 :: 28 评论 :: 92504 阅读

LeetCode: Populating Next Right Pointer 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

地址: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
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    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 };
复制代码

 

posted on   Boostable  阅读(209)  评论(0)    收藏  举报
编辑推荐:
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
阅读排行:
· 工良出品 | 长文讲解 MCP 和案例实战
· 多年后再做Web开发,AI帮大忙
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· centos停服,迁移centos7.3系统到新搭建的openEuler
· 上周热点回顾(4.14-4.20)
点击右上角即可分享
微信分享提示