[Leetcode]Populating Next Right Pointers in Each Node II
来源:互联网 发布:java中的迭代器 编辑:程序博客网 时间:2024/05/21 06:57
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的扩展题~还是要求用常数空间,不过树不是完全二叉树~下面解法其实思路挺简单的,明白了的话写起来也会很顺手~
class Solution: # @param root, a tree node # @return nothing def connect(self, root): cur, head, prev = root, None, None while cur: while cur: if cur.left: if prev: prev.next = cur.left else: head = cur.left prev = cur.left if cur.right: if prev: prev.next = cur.right else: head = cur.right prev = cur.right cur = cur.next cur = head prev, head = None, None
0 0
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- [leetcode] Populating Next Right Pointers in Each Node II
- LeetCode-Populating Next Right Pointers in Each Node II
- LeetCode - Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node II
- LeetCode | Populating Next Right Pointers in Each Node II
- [leetcode]Populating Next Right Pointers in Each Node II
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- 医药企业信息安全受黑客威胁,将为企业造成具大损失!
- 开源数据库,SQLite 3.8.8 发布.()
- 【读书笔记】《推荐系统(recommender systems An introduction)》第七章 评估推荐系统
- 企业安全需要全面的整体安全解决方案
- Python 一个简单的python socket编程
- [Leetcode]Populating Next Right Pointers in Each Node II
- 软件工程各阶段的评审内容
- 【读书笔记】《推荐系统(recommender systems An introduction)》第八章 案例分析:移动互联网个性化游戏推荐
- 企业数据安全修补漏洞的三个步骤
- CButtonST使用技巧(三)
- MySQL/MariaDB 安装启动及设置和ERROR 1130 解决方法
- 【读书笔记】《推荐系统(recommender systems An introduction)》第九章 针对协同过滤推荐系统的攻击
- 企业信息安全漏洞大多由于应用软件造成
- 【BST】把两棵BST合并成一棵高度平衡的BST