LeetCode - Populating Next Right Pointers in Each Node
2014-01-08 06:43
351 查看
Populating Next Right Pointers in Each Node
2014.1.8 05:59
Given a binary tree
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
Initially, all next pointers are set to
Note:
You may only use constant extra space.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
After calling your function, the tree should look like:
Solution:
It looks like this problem is somewhat related to level-order traversal, but the space limit cannot meet the requirement. You can't use constant space to do a level-order traversal. So we'll do it the preorder way.
Left node is next to the right node, while the right node is next to the leftest node you can find on the same level right to this right node.
Well... I guess the code is better explanation for this problem. I wouldn't waste more words than code to show what I'm doing. Please see the codes below.
Time and space complexities are both O(n), where n is the number of nodes in the tree.
Accepted code:
2014.1.8 05:59
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.
Note:
You may only use constant extra space.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
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
Solution:
It looks like this problem is somewhat related to level-order traversal, but the space limit cannot meet the requirement. You can't use constant space to do a level-order traversal. So we'll do it the preorder way.
Left node is next to the right node, while the right node is next to the leftest node you can find on the same level right to this right node.
Well... I guess the code is better explanation for this problem. I wouldn't waste more words than code to show what I'm doing. Please see the codes below.
Time and space complexities are both O(n), where n is the number of nodes in the tree.
Accepted code:
// 1AC, nice /** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */ class Solution { public: void connect(TreeLinkNode *root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(root == nullptr){ return; } // This algorithm won't work if the tree is not perfect. if(root->left){ TreeLinkNode *ll, *rr; ll = root->left; rr = root->right; while(ll != nullptr){ ll->next = rr; ll = ll->right; rr = rr->left; } } connect(root->left); connect(root->right); } };
相关文章推荐
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- [Leetcode] Populating Next Right Pointers in Each Node II (Java)
- LeetCode OJ Populating Next Right Pointers in Each Node
- [leetcode 116] Populating Next Right Pointers in Each Node
- leetcode -- Populating Next Right Pointers in Each Node I&II -- 再看简单常考
- LeetCode | Populating Next Right Pointers in Each Node
- leetcode-Populating Next Right Pointers in Each Node II
- leetcode_117_Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- [LeetCode]116 Populating Next Right Pointers in Each Node
- 【leetcode】Populating Next Right Pointers in Each Node II
- LeetCode_Tree_Populating Next Right Pointers in Each Node I II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode_populating-next-right-pointers-in-each-node
- [LeetCode]Populating Next Right Pointers in Each Node II, 解题报告
- Populating Next Right Pointers in Each Node II [Leetcode]
- LeetCode "Populating Next Right Pointers in Each Node II"
- leetcode117~Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node LeetCode