Posted on 2014-01-10 18:31
Uriel 閱讀(94)
評論(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 };