当前位置:网站首页>Leetcode 116. fill in the next right node pointer of each node
Leetcode 116. fill in the next right node pointer of each node
2022-07-22 18:49:00 【Great white sheep_ Aries】
Title Description
116. Fill in the next right node pointer for each node
solution :
First solve it recursively , Our approach is to connect every two adjacent nodes , That's it n o d e 1. l e f t → n o d e 1. r i g h t node1.left\rightarrow node1.right node1.left→node1.right , n o d e 2. l e f t → n o d e 2. r i g h t node2.left\rightarrow node2.right node2.left→node2.right, n o d e 1. r i g h t → n o d e 2. l e f t node1.right\rightarrow node2.left node1.right→node2.left
/* // Definition for a Node. class Node { public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} Node(int _val, Node* _left, Node* _right, Node* _next) : val(_val), left(_left), right(_right), next(_next) {} }; */
class Solution {
public:
Node* connect(Node* root) {
if (root == nullptr) return root;
connectTwoNode(root->left, root->right);
return root;
}
void connectTwoNode(Node* node1, Node* node2){
if (node1 == nullptr || node2 == nullptr) return;
node1->next = node2;
connectTwoNode(node1->left, node1->right);
connectTwoNode(node2->left, node2->right);
connectTwoNode(node1->right, node2->left);
}
};
边栏推荐
- Summary 20220118 (binary tree)
- Go language learning: go language journey (III)
- 1.针对QDate()的日期指向那边, 2.QT_VERSION的用法总结
- Recursively find the partial sum of simple alternating power series (15 points)
- PTA 6-11 求自定类型元素序列的中位数 (25 分)
- 总结20220118(二叉树)
- 01. Open closed principle
- LeetCode: 620. 有趣的电影
- Go 语言学习:Go 语言之旅(三)
- 1.QTableWidget插入按钮,灵活删除本行,一列显示行号
猜你喜欢
连接mysql8.0出现caching-sha2-password问题
Important knowledge points of go language: string, UTF-8 encoding, Rune
[FatFs] FAT32 file system protocol summary (Theory + Practice)
App mobile terminal test [6] application program (APK) package management and activity
TCP and UDP, three handshakes and four waves
Summary 20220121
模块TensorFlow中没有Session
MySQL的语句执行顺序
Recursively find the partial sum of simple alternating power series (15 points)
ps: 如何调出辅助线
随机推荐
Go language learning: go language journey (4)
Qt | 模態對話框和非模態對話框 QDialog
Summary 20220119
1.QTableWidget的closable,2.pro/build_pass、member,3.QString&&
Protocol and port
六度空间
1.通过类似window路径的方式访问json
1.qt 查看源码
04. interface aggregation principle
总结20220120
PTA习题8-6 删除字符
bjyx
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
[Nordic] nrf52810 OTA upgrade (II) – how to use DFU
NRF24L01 wireless module setting transmit receive mode method
STM32+ESP8266+MQTT协议连接OneNet物联网平台
Summary 20220209
递归求简单交错幂级数的部分和 (15分)
App移動端測試【6】應用程序(apk)包管理與activity
04.接口隔离原则(Interface Segregation Principle)