Populating Next Right Pointers in Each Node II
2015-11-14 14:57
549 查看
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:
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; * 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; outside(root->left,root->right); connect(root->left); connect(root->right); } void outside(TreeLinkNode* l,TreeLinkNode* r){ if(l==NULL||r==NULL) return; else l->next=r; outside(l->left,r->right); outside(l->left,r->left); outside(l->right,r->right); outside(l->right,r->left); } };
相关文章推荐
- Populating Next Right Pointers in Each Node
- hadoop namenode启动报Permission denied
- Leetcode Swap Nodes in Pairs
- 使用nvm做nodejs的版本管理
- LeetCode19:Remove Nth Node From End of List
- [leetcode 19] Remove Nth Node From End of List
- LeetCode 24_Swap Nodes in Pairs
- Ubuntu下安装Node.js
- NodeJs端口被占用的情况
- 删除 windows 下 node_modules 过深的目录
- 【leetcode】【24】Swap Nodes in Pairs
- 初识node.js
- node -e "require('nan')" npm 安装 tty.js 时报错 ,解决方法
- 关于机房交换机故障导致HDFS NameNode挂掉的问题
- 一步步学习使用node和express开发web应用(二)——npm介绍,express的引入,路由机制
- Node.js
- Node.js cookie-parser
- 一步步学习使用node和express开发web应用(一)——node简介、开发环境及构建最简web应用
- PRVF-0002 : Could not retrieve local nodename 报错解决
- node文章