117. Populating Next Right Pointers in Each Node II Leetcode Python
来源:互联网 发布:最新区域名紧急通知 编辑:程序博客网 时间:2024/06/06 00:47
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
/ \ \
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
这题和原来题目的区别在于这里的tree有肯能不是full tree.或者有残缺。 所以最好的方法是level order travesral然后把每层的node从左边指向右边。
这里用了个额外的存储。所以空间复杂度为O(N)
一共需要遍历两遍第一遍是存储,第二遍是指针。所以时间复杂度为O(N)
code is as follow
# 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 solve(self,solution,level,root): if len(solution)<level+1: solution.append([]) solution[level].append(root) if root.left: self.solve(solution,level+1,root.left) if root.right: self.solve(solution,level+1,root.right) def connect(self, root): if root==None: return root solution=[] level=0 self.solve(solution,level,root) for level in range(len(solution)): for count in range(1,len(solution[level])): solution[level][count-1].next=solution[level][count] solution[level][-1].next=None return root
another method need O(n) time and O(1) space
# Definition for binary tree with next pointer.# class TreeLinkNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = None# self.next = Noneclass Solution: # @param root, a tree link node # @return nothing def connect(self, root): cur=root while cur: pre=None firstnode=None while cur: if firstnode==None: firstnode= cur.left if cur.left else cur.right if cur.left: if pre: pre.next=cur.left pre=cur.left if cur.right: if pre: pre.next=cur.right pre=cur.right cur=cur.next cur=firstnode
0 0
- LeetCode 117. Populating Next Right Pointers in Each Node II
- [LeetCode]117.Populating Next Right Pointers in Each Node II
- [Leetcode] 117. Populating Next Right Pointers in Each Node II
- [leetcode] 117.Populating Next Right Pointers in Each Node II
- [LeetCode]117. Populating Next Right Pointers in Each Node II
- Leetcode 117. Populating Next Right Pointers in Each Node II
- [leetcode]117. Populating Next Right Pointers in Each Node II
- LeetCode 117. Populating Next Right Pointers in Each Node II
- Leetcode 117. Populating Next Right Pointers in Each Node II
- [LeetCode] 117. Populating Next Right Pointers in Each Node II
- LeetCode 117. Populating Next Right Pointers in Each Node II
- leetcode-117. Populating Next Right Pointers in Each Node II
- Leetcode 117. Populating Next Right Pointers in Each Node II
- leetcode 117. Populating Next Right Pointers in Each Node II
- LeetCode 117. Populating Next Right Pointers in Each Node II
- leetcode 117. Populating Next Right Pointers in Each Node II
- [LeetCode] 117. Populating Next Right Pointers in Each Node II
- LeetCode#117. Populating Next Right Pointers in Each Node II
- hadoop超时解决办法context.progress()的作用 假设在map()方法中,你有一个从数据库读取大量数据的操作,是用一个循环来完成的,并且,在读完全部的数据之前,你不会有任何的数据输出
- unity3d 截屏
- android工程师的水平问题
- Linux系统启动时间的优化
- iOS 单例创建
- 117. Populating Next Right Pointers in Each Node II Leetcode Python
- GTK常用控件之行编辑( GtkEntry )
- nyoj-16-动态规划之最长路-嵌套矩形
- 基于Android2.3.5系统:Android init.rc文件浅析
- 如何把一个java工程打成一个jar包
- android ant 自动批量打包——带有第三方项目library的情况(补充运行)
- php Curl 里面模拟表单提交 文本+文件的写法
- Android系统自带样式(@android:style/)
- Tomcat 系统架构与设计模式