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

leetcode:Populating Next Right Pointers in Each Node

2014-06-15 14:59 169 查看




显然可以知道,一个节点的left节点的next指向节点right节点,right节点指向该节点next节点的left节点

/**
* 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 ;
}
else{
if(root.left != null){
root.left.next = root.right;
}
if(root.right != null){
root.right.next = (root.next == null ? null : root.next.left);
}
connect(root.left);
connect(root.right);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: