zoukankan      html  css  js  c++  java
  • Java for LeetCode 116 Populating Next Right Pointers in Each Node

    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);
        }
    
  • 相关阅读:
    【bzoj1010】[HNOI2008]玩具装箱toy
    bzoj 3173
    bzoj 1179
    bzoj 2427
    bzoj 1051
    bzoj 1877
    bzoj 1066
    bzoj 2127
    bzoj 1412
    bzoj 3438
  • 原文地址:https://www.cnblogs.com/tonyluis/p/4525634.html
Copyright © 2011-2022 走看看