leetcode[117]:Populating Next Right Pointers in Each Node II
2015-07-26 16:35
671 查看
Populating Next Right Pointers in Each Node II
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,
After calling your function, the tree should look like:
先操作右边!!!否则可能左边找右链接右边还没找到!!!
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
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * struct TreeLinkNode *left, *right, *next; * }; * */ void connect(struct TreeLinkNode *root) { int i; struct TreeLinkNode *tmp; if(!root) return; if(root->left) { if(root->right) root->left->next=root->right; else if(root->next) { tmp=root->next; while(tmp) { if(tmp->left) { root->left->next=tmp->left; break; } if(tmp->right) { root->left->next=tmp->right; break; } tmp=tmp->next; } } } if(root->right) { if(root->next) { tmp=root->next; while(tmp) { if(tmp->left) { root->right->next=tmp->left; break; } if(tmp->right) { root->right->next=tmp->right; break; } tmp=tmp->next; } } } connect(root->right); connect(root->left); }
先操作右边!!!否则可能左边找右链接右边还没找到!!!
相关文章推荐
- 命令行快速技巧:如何定位一个文件
- cmd tree命令 以树形格式罗列文件
- 推荐8款jQuery轻量级树形Tree插件
- tree 以树形格式罗列文件
- EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例
- E3 tree 1.6在Firefox下显示问题的修复方法
- js树形控件脚本代码
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- Spark Decision Tree
- 带check的treeView
- extjs 4 treepanel locked and expand
- JQuery EasyUI 实现tree的右键菜单
- flex tree 的两个小技巧
- Tree.log
- gnome文件管理器(nautilus)树形结构文件窗口 多窗口
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- python递归实现Easyui combotree树
- InnoDB--------查询IOT B+ Tree的高度