[LC 116.填充每个节点的下一个右侧节点指针]

递归调用

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if (root == null)
            return null;
        connectTwoNode(root.left,root.right);
        return root;

    }
    public void connectTwoNode(Node a,Node b){
        if (a == null || b == null )
            return ;
        a.next = b;
        connectTwoNode(a.left,a.right);
        connectTwoNode(a.right,b.left);
        connectTwoNode(b.left,b.right);
    }
}
全部评论

相关推荐

用微笑面对困难:985只有在应届生里面的优势是断层的在社招或者更远的工作中算是后续能力优先级
工作压力大,你会干什么?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务