[LeetCode] Populating Next Right Pointers in Each Node 深度搜索
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
#include <iostream>
using namespace std; /**
* 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||root->left==NULL) return;
connect(root->left);
help_fun(root->left,root->right);
connect(root->right);
}
void help_fun(TreeLinkNode* lft,TreeLinkNode *rgt)
{
if(lft==NULL) return ;
lft->next = rgt;
rgt->next = NULL;
help_fun(lft->right,rgt->left);
}
}; int main()
{ return ;
}
[LeetCode] Populating Next Right Pointers in Each Node 深度搜索的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
随机推荐
- 项目实战2.3-Nginx的“远方表哥”—Tengine
本文收录在Linux运维企业架构实战系列 今天想起当初研究nginx反向代理负载均衡时,nginx自身的upstream后端配置用着非常不舒服: 当时使用的淘宝基于nginx二次开发的Tengine, ...
- 15.VUE学习之-表单中使用key唯一令牌解决表单值混乱问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- Python基础:条件与循环
条件语句 除了 boolean 类型的数据,条件判断最好是显性的 if i != 0: ... 而不是只写出变量名: if i: ... For循环与While循环 通常来说,如果你只是遍历一个已知的 ...
- docker 学习(3)
docker和宿主之间的数据共享以及docker间的数据共享仍然是让人头疼和操心的地方. 几个基本概念: docker: 一种容器管理技术,这里也指既有的开发工具链. container: 容器 im ...
- Codeforces Round #472 A-D
A题: 题意:就是给你一个长度为n的字符串,有三种颜色,其中有一些‘?’的字符代表未着色,你需要找到至少有两种方法染色,同时满足相邻两个字符间不能相同: 思路:有两种染色方法的前提:首先给定的字符串中 ...
- Network of Schools POJ - 1236 (强联通)
一些学校连接到了一个计算机网络.网络中的学校间有如下约定:每个学校维护一个列表,当该学校收到软件或信息后将会转发给列表中的所有学校(也就是接收方列表).需要注意的是如果B学校在A学校的接收方列表中,A ...
- Python 代码优化技巧(一)
Table of Contents 1. 代码优化Part1 1.1. if 判断的短路特性 1.2. join 合并字符串 1.3. while 1 和 while True 1.4. cProfi ...
- P3376 【模板】网络最大流dinic算法
P3376 [模板]网络最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点 ...
- Python-S9-Day99——Web前端框架之Vue.js
01课程安排 02let和const: 03 箭头函数 04 对象的单体模式 05 Node.js介绍和npm操作 06 Webpack,babel介绍和Vue的第一个案例 01课程安排 1.1 ht ...
- PHP 与 Redis 入门教程
Redis 官方推荐的 PHP 客户端是 Predis 和 phpredis. 前者是完全使用 PHP 代码实现的原生客户端,而后者则是使用 C 语言编写的 PHP 扩展.在功能上两者区别并不大,就性 ...