LeetCode | Populating Next Right Pointers in Each Node II
2013-09-21 23:16
381 查看
题目:
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
思路:
类似/article/1382601.html。只是在寻找子节点的时候需要多一些考虑。使用递归的方法可以使代码更加简介。代码:
非递归方法:/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root != NULL) { build(root); if(root->left != NULL) { connect(root->left); } if(root->right != NULL) { connect(root->right); } } } void build(TreeLinkNode * root) { if(root != NULL) { TreeLinkNode * parent = root; TreeLinkNode * cur = NULL; TreeLinkNode * next = NULL; int isLeft = 0; do { while(cur == NULL && parent != NULL) { if(parent->left != NULL) { isLeft = 1; cur = parent->left; break; } if(parent->right != NULL) { isLeft = 2; cur = parent->right; break; } parent = parent->next; } if(isLeft == 2 && parent != NULL) { parent = parent->next; isLeft = 0; } while(next == NULL && parent != NULL) { if(parent->left != NULL && isLeft != 1) { isLeft = 1; next = parent->left; break; } if(parent->right != NULL) { isLeft = 2; next = parent->right; break; } if(isLeft == 1) { isLeft = 0; } parent = parent->next; } if(cur != NULL && next != NULL) { cur->next = next; cur = next; next = NULL; } } while(parent != NULL); } } };
递归方法:
/** * 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) { while((root = getChildren(root, NULL)) != NULL); } TreeLinkNode* getChildren(TreeLinkNode* node, TreeLinkNode* cur){ if(node == NULL){ return NULL; } TreeLinkNode* firstChild = NULL; if(node->left != NULL){ setCurrentNode(cur, node->left); if(firstChild == NULL){ firstChild = cur; } } if(node->right != NULL){ setCurrentNode(cur, node->right); if(firstChild == NULL){ firstChild = cur; } } TreeLinkNode* tmp = getChildren(node->next, cur); if(firstChild == NULL){ firstChild = tmp; } return firstChild; } void setCurrentNode(TreeLinkNode* &cur, TreeLinkNode* next){ if(cur == NULL){ cur = next; } else{ cur->next = next; cur = next; } } };
相关文章推荐
- LeetCode - Populating Next Right Pointers in Each Node I && II
- leetcode - Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- Leetcode: Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- LeetCode - Populating Next Right Pointers in Each Node II
- leetcode:Populating Next Right Pointers in Each Node II
- LeetCode(49) Populating Next Right Pointers in Each Node I II
- 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(I and II)
- LeetCode:Populating Next Right Pointers in Each Node I II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II [leetcode] 空间O(1)的基于循环和基于递归的两种方法
- LeetCode之Populating Next Right Pointers in Each Node II
- Leetcode_Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II Leetcode Python
- 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