首页 技术 正文
技术 2022年11月21日
0 收藏 705 点赞 4,044 浏览 2508 个字

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://leetcode.com/problems/populating-next-right-pointers-in-each-node/

由于空间复杂度为O(1),广搜深搜不能使用,只能考虑递归迭代。充分利用parent的next指针,我们可以很容易的找到子节点的next指针。

// C++ RECURSIVE CODE: 
class Solution {
public:
static void connect(TreeLinkNode* root){
if(root == NULL) return;
if(root->left) root->left->next = root->right;
if(root->next && root->right ) root->right->next = root->next->left;
connect(root->left);
connect(root->right);
}
};

实际上用栈也是会消耗空间的,迭代应该是最合适的办法。为了保持处理的一致性,我们可以给每一层加一个dummy头结点,然后根据parent层(利用next)决定child层的next。

// JAVA ITERATIVE CODE:
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode dummy = new TreeLinkNode(0);
while(root != null){
TreeLinkNode child = dummy;
dummy.next = null;
while(root != null){
if(root.left != null){
child.next = root.left;
child = child.next;
}
if(root.right != null){
child.next = root.right;
child = child.next;
}
root = root.next;
}
root = dummy.next;
}
}
}

PYTHON ITERATIVE CODE:

class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
dummychild = TreeLinkNode(0)
while root:
cur = dummychild
dummychild.next = None
while root:
if root.left:
cur.next = root.left
cur = cur.next
if root.right:
cur.next = root.right
cur = cur.next
root = root.next
root = dummychild.next

 

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://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
唯一的区别是找子节点的next指针不是那么直接了,可以用函数根据不同情况来返回,其他的部分做法一样。
class Solution {
public:
TreeLinkNode* first(TreeLinkNode* root){
root = root->next;
while(root){
if(root->left) return root->left;
else if(root->right) return root->right;
root = root->next;
}
return NULL;
}
void connect(TreeLinkNode* cur){
TreeLinkNode* root = cur;
if(root == NULL) return;
while(root){
if(root->left){
root->left->next = root->right ? root->right: first(root);
}
if(root->right){
root->right->next = first(root);
}
root = root->next;
}
connect(cur->left);
connect(cur->right);
}
};


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919