Leetcode_Populating Next Right Pointers in Each Node II
来源:互联网 发布:淘宝天猫入驻 编辑:程序博客网 时间:2024/05/17 12:23
Question link: http://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
Solution:
For each node, root. if all nodes that's one level above root is already connected, we can find next for root by iterating all nodes that is one level above the current node. First, I subconsciously adhere to the convention of dealing with the left child first, then the right child. that's what i just do it. One problem occurs. For a tree deep enough, not all the nodes one level above the current node is connected. Cause, the right part of the whole tree is not yet dealt with.
I thought about it for some time. Still can't come up a new idea. One simple idea is to repeat what's initial proposed several times until no 'next' field is updated.
I have to Google the answer, now.
And the answer is deal with the right tree first...
Hint:
Some times you have to beat your subconsciousness to gain a new solution.
How to beat your subconsciousness? Know your subconsciousness first. (That's another story i have learned from learning physics in high school.)
From: http://blog.csdn.net/cheetach119/article/details/12289867
- Leetcode_Populating Next Right Pointers in Each Node II
- LeetCode_populating-next-right-pointers-in-each-node
- Leetcode_populating-next-right-pointers-in-each-node-ii(updated c++ version)
- Leetcode_populating-next-right-pointers-in-each-node(updated c++ and python version)
- Populating Next Right Pointers in Each Node、Populating Next Right Pointers in Each Node II
- 【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
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- 快速排序及其JAVA实现
- Leetcode: Rotate Image
- C++解析JSON
- 大整数相乘
- java堆、栈、内存分析
- Leetcode_Populating Next Right Pointers in Each Node II
- 课本习题30页 2——6
- linux基础指令学习1
- 有序数组的合并
- java的移位运算和位运算
- 编写Mahout程序流程
- HDU 4082 Hou Yi's secret 伪相似三角形 && 2011 Asia Beijing Regional Contest
- 把字符串里面连续的数字依次存放到一个数组中
- Java代理之静态代理与动态代理(一)