[LeetCode]Populating Next Right Pointers in Each Node II
来源:互联网 发布:网络电视那个牌子的好 编辑:程序博客网 时间:2024/05/24 06:19
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
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> 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) { TreeLinkNode *ChildFrontNode = new TreeLinkNode (0);//子链表头节点 TreeLinkNode *pre = ChildFrontNode;//记录先前值,方便生成链表 while(root){ if(root->left){ pre->next = root->left; pre = pre->next; } if(root->right){ pre->next = root->right; pre = pre->next; }//两个子节点生成链表 root = root->next; // 根节点指向下一个 if(root==NULL){ root = ChildFrontNode->next; pre = ChildFrontNode; ChildFrontNode->next = NULL; //走到链表尾部,转到下一行ChildFrontNode } } }};
0 0
- 【LeetCode】Populating Next Right Pointers in Each Node 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 79: 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]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]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 OJ】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- iOS打开外部应用、浏览器
- 计算机视觉整理库
- iOS 播放视频
- popen()/pclose() -- 使进程像文件一样
- Django配置静态文件路径
- [LeetCode]Populating Next Right Pointers in Each Node II
- 三、获取寄存器信息
- 68.NSURLSession使用说明及后台工作流程分析
- 慎用PHP$_REQUEST数组
- Java并发编程:深入剖析ThreadLocal
- php中include和require的区别
- Cordova小结
- 02_01_Linux操作系统及常用命令
- ios开发----应用数据存储的常用方法之plist存取方法