mycode   93.97%

  1. """
  2. # Definition for a Node.
  3. class Node(object):
  4. def __init__(self, val, left, right, next):
  5. self.val = val
  6. self.left = left
  7. self.right = right
  8. self.next = next
  9. """
  10. class Solution(object):
  11. def connect(self, root):
  12. if not root:
  13. return None
  14.  
  15. if root and root.left:
  16. root.left.next = root.right
  17. if root.next != None:
  18. #print(root.val,root.next)
  19. root.right.next = root.next.left
  20. self.connect(root.left)
  21. self.connect(root.right)
  22. return root

参考:

其实第二个if可以只写root.left,这样阔以快一丢丢啦

  1. class Solution(object):
  2. def connect(self, root):
  3. """
  4. :type root: Node
  5. :rtype: Node
  6. """
  7. if not root:
  8. return None
  9. if root.left:
  10. root.left.next = root.right
  11. if root.next:
  12. root.right.next = root.next.left
  13. self.connect(root.left)
  14. self.connect(root.right)
  15. return root

leetcode-mid-Linked list- 116. Populating Next Right Pointers in Each Node的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  5. 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  6. LeetCode OJ 116. Populating Next Right Pointers in Each Node

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

  7. leetcode 116 Populating Next Right Pointers in Each Node ----- java

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

  8. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

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

  9. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  10. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

随机推荐

  1. HTTP/2 最新漏洞,直指 Kubernetes!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 在这个数据.应用横行的时代,漏洞的出现早已屡见不鲜.在尚未造成大面积危害之前,我们该如何做好防御措施?或许从过往经常发生漏洞 ...

  2. 3.golang 的注释

    package main import ( "fmt" "math" ) func main() { fmt.Println(pi(5000)) } // pi ...

  3. PHP 经典有趣的算法

    原文:https://blog.csdn.net/a519395243/article/details/77942913 1.一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只 ...

  4. quartz的学习和简单使用

    以前在框架中使用过,很多都是纯粹的复制粘贴,了解过用法知道如何配置,但时间久了就没什么印象了,现在重新捡起来,再次进行学习. quartz相关的介绍都已经很多了,我就不重复啰嗦,简单说一下个人的认识和 ...

  5. Qt和其它GUI库的对比

    http://c.biancheng.net/view/3876.html 世界上的 GUI 库多如牛毛,有的跨平台,有的专属于某个操作系统:有的只有 UI 功能,有的还融合了网络通信.多媒体处理.数 ...

  6. 009-流程控制 for 语句

    流程控制 for 语句 ##################### 语法一 ################################# #!/bin/bash do echo $i done ...

  7. 09java进阶——IO

    1.File类 1.1目录及路径分隔符 package cn.jxufe.java.chapter09.demo01; import java.io.File; public class Test01 ...

  8. 2017 BJ ICPC 石子合并变种 向量基本功及分类考察

    E 模拟 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) # ...

  9. 1122. Hamiltonian Cycle (25)

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  10. [HTTP知识体系]前端常用的一些参数

    1.http常见状态码(status code) 200(成功) 服务器已成功处理了请求.通常,这表示服务器提供了请求的网页. 301 (永久移动) 请求的网页已永久移动到新位置. 服务器返回此响应( ...