Java for LeetCode 116 Populating Next Right Pointers in Each Node
2015-05-24 12:12
821 查看
Given a binary tree
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
Initially, all next pointers are set to
解题思路:
直接观察即可写出递归代码,JAVA实现如下:
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.
解题思路:
直接观察即可写出递归代码,JAVA实现如下:
public void connect(TreeLinkNode root) { if(root==null||root.left==null) return; root.left.next=root.right; if(root.next!=null) root.right.next=root.next.left; connect(root.left); connect(root.right); }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- leetcode-java-116. Populating Next Right Pointers in Each Node
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
- (Java) LeetCode 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 问题 in JAVA
- leetcode || 116、Populating Next Right Pointers in Each Node
- LeetCode 116 Populating Next Right Pointers in Each Node
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
- Leetcode 116. Populating Next Right Pointers in Each Node
- leetcode_question_116 Populating Next Right Pointers in Each Node
- [LeetCode]problem 116. Populating Next Right Pointers in Each Node
- leetcode_116题——Populating Next Right Pointers in Each Node(树,广度优先搜索)
- Leetcode #116 Populating Next Right Pointers in Each Node
- LeetCode(116) Populating Next Right Pointers in Each Node
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- Leetcode#116 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 #116 - Populating Next Right Pointers in Each Node - Medium