leetcode JAVA Populating Next Right Pointers in Each Node 难度系数3 3.41
来源:互联网 发布:lua编程入门 编辑:程序博客网 时间:2024/06/07 02:42
Question:
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *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
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
/** * 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) { connect(root,null); }private void connect(TreeLinkNode root, TreeLinkNode nextTo) {if(root==null){return;}else{root.next = nextTo;}connect(root.left,root.right);if(nextTo!=null){connect(root.right,nextTo.left);}else{connect(root.right,null);}}}
0 0
- leetcode JAVA Populating Next Right Pointers in Each Node 难度系数3 3.41
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- [Leetcode] Populating Next Right Pointers in Each Node (Java)
- [Leetcode] Populating Next Right Pointers in Each Node II (Java)
- [LeetCode][Java] Populating Next Right Pointers in Each Node
- [LeetCode][Java] Populating Next Right Pointers in Each Node II
- leetcode:Populating Next Right Pointers in Each Node 【Java】
- leetcode:Populating Next Right Pointers in Each Node II 【Java】
- Populating Next Right Pointers in Each Node Python Java Leetcode
- leetcode-java-116. Populating Next Right Pointers in Each Node
- 【leetcode】116. Populating Next Right Pointers in Each Node【java】
- LeetCode-116. Populating Next Right Pointers in Each Node (JAVA)
- LeetCode-117. Populating Next Right Pointers in Each Node || (JAVA)
- 【LeetCode】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||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- 2014-2-5 随笔
- poj 1584 A Round Peg in a Ground Hole(计算几何)
- Android第一个apk之HelloWorld
- leetcode JAVA Flatten Binary Tree to Linked List 难度系数3 3.40
- Android软件开发之获取通讯录联系人信息
- leetcode JAVA Populating Next Right Pointers in Each Node 难度系数3 3.41
- 用O(lgn)时间求出两个已排序数组的中位数
- GetOrdValue
- WPF DataGrid
- leetcode JAVA Triangle 难度系数3 3.42
- Asp.net总结(一)ViewState
- VS13下遇到的编译错误:cl.exe exited with code 2
- IO高级编程——获取特定位置上的数据(lseek函数的使用)
- 利用NSBundle加载资源文件