Populating Next Right Pointers in Each Node II
来源:互联网 发布:怎么健身 知乎 编辑:程序博客网 时间:2024/05/01 10:16
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 -> NULLJava代码:
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; TreeLinkNode left = root.left; TreeLinkNode right = root.right; connect(left); connect(right); connect(left, right);}public void connect(TreeLinkNode left, TreeLinkNode right) { if (left == null) return; if (left != null && right != null) { connect(left.right, right.left); connect(left.right, right.right); connect(left.left, right.left); connect(left.left, right.right); if (left.next == null) left.next = right; }}}
Double recursion
:http://en.wikipedia.org/wiki/Double_recursion 0 0
- 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
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- 新建Maven+Spring项目错误集锦
- uva 11384 Help is needed for Dexter(模拟)
- [Java]Eclipse配置JDK的图解步骤
- USACO1.2.4 Palindromic Squares (palsquare)
- Codeforces Round #276 (Div. 2) Factory
- Populating Next Right Pointers in Each Node II
- Java 对象数组初始化 和 基本类型数组初始化的区别
- Activity的启动模式
- 下位机单片机c语言发送数据到串口,上位机pc机java语言获取端口数据
- Serial Port 波形分析
- android5 sdk 下载地址大全
- 开源中国iOS客户端涉及的知识点(下)
- dynamic programming in Date Structures and Algorithm Analysis in C
- USACO1.2.5 Dual Palindromes (dualpal)