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

点击打开原题链接

基本上是层次遍历稍微修改下就可以了,算法比较简单,注意下边界条件即可,也可以用把每一层放入vector<vector<> >中去,但是会开辟新的内存,这里

直接判断是否在一层上,代码稍微复杂一点点。

struct TreeLinkNode
 {
	  int val;
	  TreeLinkNode *left, *right, *next;
	  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
	};
 struct node
 {
	 int level;
	 TreeLinkNode* TLN;
 };

	class Solution
	{
	public:
		void connect(TreeLinkNode *root)
		{
			if (root == NULL)
			{
				return;
			}
			deque<node> deque_TreeLinkNode;
			node pre,current;
			pre.level = 0;
			pre.TLN = root;
			deque_TreeLinkNode.push_back(pre);
			pre.TLN = NULL;
			while(!deque_TreeLinkNode.empty())
			{
				node temp = deque_TreeLinkNode.front();

				if (temp.TLN->left != NULL)
				{
					node left;
					left.level = temp.level+1;
					left.TLN = temp.TLN->left;
					deque_TreeLinkNode.push_back(left);
				}
				if (temp.TLN->right != NULL)
				{
					node right;
					right.level = temp.level+1;
					right.TLN = temp.TLN->right;
					deque_TreeLinkNode.push_back(right);
				}

				current= deque_TreeLinkNode.front();
				deque_TreeLinkNode.pop_front();
				if (pre.TLN == NULL)
				{
					pre.TLN = current.TLN;
					pre.level = current.level;
				//	deque_TreeLinkNode.pop_front();
					continue;
				}
				else
				{
					if (current.level == pre.level)
					{

						pre.TLN->next = current.TLN;
						pre=current;
						continue;
					//	current->TLN = NULL;
					//	current->level = 0;
					}
					else
					{
						pre.TLN->next = NULL;
						pre = current;
					}

				}

			}
			pre.TLN->next = NULL;

		}
	private:

	};
时间: 2024-10-28 06:31:04

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

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

LeetCode: Populating Next Right Pointers in Each Node II [117]

[题目] 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 bin

Problem Populating Next Right Pointers in Each Node II

Problem Description: 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 th