leetcode_117_Populating Next Right Pointers in Each Node II

描述:

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,

Given the following binary tree,

         1
       /        2    3
     / \        4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \        4-> 5 -> 7 -> NULL

思路:

就是用两个list存储相邻两层的结点,把第i+1层结点存储到list中的同时,分别将第i层的各个节点指向下一个,跟116题Populating Next Right Pointers in Each Node I一个思路,就不重复写了。

代码:

/**
 * 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 ;
	      List<TreeLinkNode>list=new ArrayList<TreeLinkNode>();
	      List<TreeLinkNode>temp=new ArrayList<TreeLinkNode>();
	      TreeLinkNode node=null,tempNode=null;
	      list.add(root);
	      while(true)
	      {
	    	  tempNode=list.get(0);
	    	  if(tempNode.left!=null)
	    		  temp.add(tempNode.left);
	    	  if(tempNode.right!=null)
	    		  temp.add(tempNode.right);
	    	  for(int i=1;i<list.size();i++)
	    	  {
	    		  node=list.get(i);
	    		  if(node.left!=null)
		    		  temp.add(node.left);
		    	  if(node.right!=null)
		    		  temp.add(node.right);
		    	  tempNode.next=node;
		    	  tempNode=node;
	    	  }
	    	  if(temp.size()!=0)
	    	  {
	    		 list=temp;
	    		 temp=new ArrayList<TreeLinkNode>();
	    	  }
	    	  else
	    		  break;
	      }
	      return;
	 }
}

结果:

时间: 2024-10-06 17:54:37

leetcode_117_Populating Next Right Pointers in Each Node II的相关文章

leetcode--Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

Populating Next Right Pointers in Each Node II LeetCode

Populating Next Right Pointers in Each Node II Total Accepted: 18934 Total Submissions: 62031My Submissions Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous

【57】117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Description Submission Solutions Add to List Total Accepted: 84339 Total Submissions: 251330 Difficulty: Medium Contributors: Admin Follow up for problem "Populating Next Right Pointers in Each Node

【leetcode】Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra s

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra sp

LeetCode——Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

Populating Next Right Pointers in Each Node II leetcode java

题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bina

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

#Leet Code# Populating Next Right Pointers in Each Node II

描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: 1 class Solution: 2 # @param root, a tree node 3 # @return nothing 4 def doSth(self, nextNode, conNode): 5 while nextNode is not