Posted on 2014-01-10 18:31
Uriel 阅读(90)
评论(0) 编辑 收藏 引用 所属分类:
LeetCode
这题丢了几天没理解题意,一开始WA了都没明白怎么回事,后来搜了一下解题报告才明白题意
题目本身还是挺简单的~直接找右边一个节点就行,而且这题保证了是完全二叉树,于是很多东西都不用考虑了,II那题是这题的加强版~
1 /**
2 * Definition for binary tree with next pointer.
3 * struct TreeLinkNode {
4 * int val;
5 * TreeLinkNode *left, *right, *next;
6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 void connect(TreeLinkNode *root) {
12 if(root == NULL) return;
13 if(root->left != NULL) {
14 root->left->next = root->right;
15 connect(root->left);
16 }
17 if(root->right != NULL) {
18 if(root->next == NULL) root->right->next = NULL;
19 else
20 root->right->next = root->next->left;
21 connect(root->right);
22 }
23 }
24 };