博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]Populating Next Right Pointers in Each Node II @ Python
阅读量:6161 次
发布时间:2019-06-21

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

原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-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

解题思路:和"Populating Next Right Pointers in Each Node"这道题不同的一点是,这道题的二叉树不是满的二叉树,有些节点是没有的。但是也可以按照递归的思路来完成。在编写递归的基准情况时需要将细节都考虑清楚:

代码一:

# Definition for a  binary tree node# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None#         self.next = Noneclass Solution:    # @param root, a tree node    # @return nothing    def connect(self, root):        if root:            if root.left and root.right:                root.left.next = root.right                tmp = root.next                while tmp:                    if tmp.left: root.right.next = tmp.left; break                    if tmp.right: root.right.next = tmp.right; break                    tmp = tmp.next            elif root.left:                tmp = root.next                while tmp:                    if tmp.left: root.left.next = tmp.left; break                    if tmp.right: root.left.next = tmp.right; break                    tmp = tmp.next            elif root.right:                tmp = root.next                while tmp:                    if tmp.left: root.right.next = tmp.left; break                    if tmp.right: root.right.next = tmp.right; break                    tmp = tmp.next            self.connect(root.right)            self.connect(root.left)            # @connect(root.right)should be the first!!!

代码二:

思路更加精巧,代码更加简洁。

# Definition for a  binary tree node# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None#         self.next = Noneclass Solution:    # @param root, a tree node    # @return nothing    def connect(self, root):        if root:            p = root; q = None; nextNode = None            while p:                if p.left:                    if q: q.next = p.left                    q = p.left                    if nextNode == None: nextNode = q                if p.right:                    if q: q.next = p.right                    q = p.right                    if nextNode == None: nextNode = q                p = p.next            self.connect(nextNode)

 

 

转载地址:http://wyafa.baihongyu.com/

你可能感兴趣的文章
Log4J日志配置详解
查看>>
实验7 BindService模拟通信
查看>>
scanf
查看>>
Socket编程注意接收缓冲区大小
查看>>
SpringMVC初写(五)拦截器
查看>>
检测oracle数据库坏块的方法
查看>>
SQL server 安装教程
查看>>
Linux下ftp和ssh详解
查看>>
跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击
查看>>
js时间和时间戳之间如何转换(汇总)
查看>>
js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用
查看>>
java中string和int的相互转换
查看>>
P1666 前缀单词
查看>>
HTML.2文本
查看>>
Ubuntu unity安装Indicator-Multiload
查看>>
解决Eclipse中新建jsp文件ISO8859-1 编码问题
查看>>
7.对象创建型模式-总结
查看>>
【论文阅读】Classification of breast cancer histology images using transfer learning
查看>>
移动端处理图片懒加载
查看>>
jQuery.on() 函数详解
查看>>