博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode116. Populating Next Right Pointers in Each Node(思路及python解法)
阅读量:2242 次
发布时间:2019-05-09

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

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

struct Node {  int val;  Node *left;  Node *right;  Node *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.


注意保存下一层最左面的点,也就是next。

剩下就是判断当前点cur是否是这一层最右面的点。

总体还是比较简单的。

class Solution:    def connect(self, root: 'Node') -> 'Node':        if not root:return root        cur = root        next = root.left                while cur.left:            cur.left.next = cur.right            if cur.next:                cur.right.next=cur.next.left                cur=cur.next            else:                cur=next                next=cur.left        return root

 

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

你可能感兴趣的文章
Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析),兼容Windows和Linux
查看>>
JAVA读取PROPERTIES配置文件
查看>>
Linux中执行shell脚本的4种方法总结
查看>>
BufferedInputStream(缓冲输入流)详解
查看>>
修改linux文件权限命令:chmod
查看>>
Linux vi/vim编辑器常用命令与用法总结
查看>>
如何使用Git Bash Here,将本地项目传到github上
查看>>
eclipse git控件操作 回退到历史提交 重置 删除(撤销)历史的某次提交
查看>>
Oracle | 给表和字段添加注释
查看>>
java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
查看>>
Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence
查看>>
java中System.exit()方法
查看>>
在hbase shell中过滤器的简单使用
查看>>
java静态方法和实例方法
查看>>
java多线程并发去调用一个类的静态方法,会有问题吗?
查看>>
关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
查看>>
Java命令学习系列(一)——Jps
查看>>
java如何计算程序运行时间
查看>>
Java Calendar 类的时间操作
查看>>
Java]NIO:使用Channel、Charset(字符集)、使用Charset传递CharBuffer
查看>>