LeetCode--Populating Next Right Pointers in Each Node
2014-08-22 21:12
411 查看
由于题目意思是满二叉树:
所以,对当前节点,设置它的左右子节点的next指针即可
root->left->next = root->right
root->right->next = root->next?root->next->left:NULL
所以,对当前节点,设置它的左右子节点的next指针即可
root->left->next = root->right
root->right->next = root->next?root->next->left: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) { if(root == NULL) return; if(root->left != NULL){ root->left->next = root->right; } if(root->right != NULL){ root->right->next = root->next?root->next->left:NULL; } connect(root->left); connect(root->right); } };
相关文章推荐
- [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
- LeetCode: Populating Next Right Pointers in Each Node
- 【leetcode】Populating Next Right Pointers in Each Node 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
- leetcode 79: Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node
- LeetCode(Oct28'12):Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- [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
- leetcode Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II