LeetCode 116. Populating Next Right Pointers in Each Node

Given the following perfect binary tree,

         1
       /        2    3
     / \  /     4  5  6  7

After calling your function, the tree should look like:

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

ver0:
 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if(root ==  NULL) return;
 5         root->left->next = root->right;//ERROR
 6         if(root->next) root->right->next = root->next->left;
 7         connect(root->left); connect(root->right);
 8         root->next = NULL;
 9     }
10 };

RA:{0} root->left right均为NULL。

ver1:28ms

 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if(root ==  NULL) return;
 5         if(root->left && root->right){
 6             root->left->next = root->right;
 7             if(root->next) root->right->next = root->next->left;
 8             connect(root->left); connect(root->right);
 9         }
10         else return;
11     }
12 };

ver2:24ms

 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if(root ==  NULL) return;
 5         if(root->left && root->right){
 6             root->left->next = root->right;
 7             if(root->next) root->right->next = root->next->left;
 8
 9         }
10         connect(root->left); connect(root->right);
11         // else return;
12     }
13 };
 
时间: 2024-10-09 22:34:13

LeetCode 116. Populating Next Right Pointers in Each Node的相关文章

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

leetcode 116 Populating Next Right Pointers in Each Node ----- java

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, al

[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, a

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, al

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

【56】116. Populating Next Right Pointers in Each Node

116. Populating Next Right Pointers in Each Node Description Submission Solutions Add to List Total Accepted: 118451 Total Submissions: 321021 Difficulty: Medium Contributors: Admin Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLi

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