原题地址: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 = None class 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 = None class 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)

[leetcode]Populating Next Right Pointers in Each Node II @ Python的更多相关文章

  1. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

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

  2. [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 ...

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

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

  5. [LeetCode] [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 ...

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

  7. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  8. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

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

  9. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

随机推荐

  1. 五、django rest_framework源码之版本控制剖析

    1 绪论 Djangorest_framework的版本控制允许用户更改不同客户端之间的行为,且提供了许多不同的版本控制方案.版本控制由传入的客户端请求确定,可以基于请求URL,也可以基于请求标头. ...

  2. 【原创】获取MySQL crash 时的core file

    最近有台服务器的MySQL经常crash,为了进一步定位问题,开启了mysql core file功能,开启步骤如下,供参考 [开启步骤] 1. my.cnf文件中增加2个配置选项 [mysqld] ...

  3. [ 转载 ] Centos安装Mysql数据库纪录

    yum install mysql-community-server依赖关系错误 https://www.cnblogs.com/lzj0218/p/5724446.html rpm -qa|grep ...

  4. hdu 3061 最大权闭合子图

    属于模板题吧... #include <cstdio> #include <cstring> #include <vector> #define min(a,b) ...

  5. Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题

    B. Bear and Finding Criminals 题目连接: http://www.codeforces.com/contest/680/problem/B Description Ther ...

  6. Codeforces Round #283 (Div. 2) A. Minimum Difficulty 暴力水题

    A. Minimum Difficulty time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. INFORMATION_SCHEMA.COLUMNS-表的字段信息

    当前数据库中当前用户可以访问的每一个列在该视图中占一行.INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.sy ...

  8. oracle开发学习篇之集合函数

    集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...

  9. PostgreSQL教程收集(中文文档/命令行工具/常用命令)

    http://www.postgres.cn/docs/9.6/index.html(中文文档) https://www.postgresql.org/docs/10/static/auth-meth ...

  10. IntelliJ IDEA部署tomcat时Edit Configuration无artifact选项

    IntelliJ使用 ##使用IntelliJ IDEA配置web项目时,选择Edit Configration部署Tomcat的Deployment可能会出现以下情况: 导致新手部署过程中摸不着头脑 ...