您的位置:首页 > Web前端 > Node.js

Java for LeetCode 116 Populating Next Right Pointers in Each Node

2015-05-24 12:12 821 查看
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
.

解题思路:

直接观察即可写出递归代码,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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: