LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点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

指出每个节点的下一个右侧节点,前一篇博客的那个思路也可以继续使用,但由于这里的二叉树是任意的,所以应该用函数找到下一层的开始节点以及下一层的下一个节点,代码如下:

 1 class Solution{
 2 public:
 3     void connect(TreeLinkNode *root)
 4     {
 5         TreeLinkNode * prev, * curr, * start;
 6         if(!root) return;
 7         while(root){
 8             start = findNextLevelStartNode(root);
 9             prev = start;
10             curr = findNextLevelNextNode(root, prev);
11             while(curr){
12                 prev->next = curr;
13                 prev = curr;
14                 curr = findNextLevelNextNode(root, curr);
15             }
16             root = start;
17         }
18     }
19 private:
20     TreeLinkNode * findNextLevelNextNode(TreeLinkNode * & node, TreeLinkNode * curr)//注意使用引用
21     {
22         if(node->left == curr && node->right)
23             return node->right;
24         else{
25             while(node->next){
26                 node = node->next;
27                 if(node->left != NULL && node->left != curr) return node->left;
28                 if(node->right != NULL && node->right != curr) return node->right;
29             }
30         }
31         return NULL;
32     }
33
34     TreeLinkNode * findNextLevelStartNode(TreeLinkNode * node)
35     {
36         if(!node) return NULL;
37         if(node->left)
38             return node->left;
39         else return findNextLevelNextNode(node, node->left);
40     }
41 };
时间: 2024-08-05 17:32:49

LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)的相关文章

LeetCode[Tree]: 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

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

Leetcode 树 Populating Next Right Pointers in Each Node

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node Total Accepted: 13657 Total Submissions: 39540 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }

leetcode -day14 Populating Next Right Pointers in Each Node I II

1.  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

[LeetCode][JavaScript]Populating Next Right Pointers in Each Node

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

【LeetCode】 Populating Next Right Pointers in Each Node 完全二叉树

题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每个节点增加一个next指针,指向树状结构排列时自己的右边节点,如果右边没有节点则置为null * * Definition for binary tree with next pointer

【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每一个节点添加一个next指针.指向树状结构排列时自己的右边节点,假设右边没有节点则置为null * * Definition for binary tree with next pointe

Java for 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 t

leetcode 117 Populating Next Right Pointers in Each Node II ----- 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 binary tr

Leetcode 116. Populating next right pointers in each node I and II

题目1: 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. Initiall