码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 116 Populating Next Right Pointers in Each Node

时间:2015-05-24 14:05:02      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }

 

Java for LeetCode 116 Populating Next Right Pointers in Each Node

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4525634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!