[Leetcode]Populating Next Right Pointers in Each Node
来源:互联网 发布:教学软件下载 编辑:程序博客网 时间:2024/06/06 17:04
Populating Next Right Pointers in Each Node My Submissions Question
Total Accepted: 72104 Total Submissions: 198545 Difficulty: Medium
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
You may only use constant extra space.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
Subscribe to see which companies asked this question
一道层序遍历的题目,没啥难度
/** * 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) return; queue<TreeLinkNode* > q; q.push(root); while(!q.empty()){ vector<TreeLinkNode*> vT; for(int i = q.size() - 1;i >= 0;-- i){ auto node = q.front(); q.pop(); vT.push_back(node); if(node->left) q.push(node->left); if(node->right) q.push(node->right); } for(int j = 0;j + 1 <= vT.size() - 1;++ j){ vT[j]->next = vT[j + 1]; } } return; }};
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [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
- git安装
- 避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常
- Drools6.1规则引擎环境搭建
- CSS3 Box-shadow
- pdf转png
- [Leetcode]Populating Next Right Pointers in Each Node
- 表单中有图文编辑器的内容,提交失败。
- Android Studio基础之生成Apk(六)-学习篇
- LeetCode---Permutations II
- linux dup,dup2,dup3 复制一个文件描述符
- OpenGL纹理贴图相关基本概念
- ASPxPopupControl 关闭刷新
- 同一个应用安装时的类型却不同
- iOS拨打电话功能的实现