[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 ...
随机推荐
- python字典按照k,v来排序
按照 k 排序 按照 v 排序
- Javascript 模块化指北
前言 随着 Web 技术的蓬勃发展和依赖的基础设施日益完善,前端领域逐渐从浏览器扩展至服务端(Node.js),桌面端(PC.Android.iOS),乃至于物联网设备(IoT),其中 JavaScr ...
- 与SVN相关的程序的调试问题【转】
解决eclipse中出现Resource is out of sync with the file system问题. 分析:有时候因为时间紧迫的原因,所以就没去管它,今天再次遇到它,实在看着不爽,所 ...
- js中123==123为false的问题--写成123=="123"即可解决问题
项目中遇到过一个问题,js拿到后台返回的一个数字,在if中判断时,出现类似123==123为false的结果, 初步分析原因,应该是返回的是string类型的,但拿来比较的是个number类型的,所以 ...
- Spring进阶-怎样集成定时调度Quartz
在一些项目里面比如进销存系统,对一些过期图片的定时清理或者库存不足发出预警提示,就需要用到定时调度技术. 每当经过一段时间,程序会自动执行,就是定时调度.如果要使用定时调度,则必须保证程序始终运行才行 ...
- Python框架之Django学习笔记(一)
Django历史: Django 是从真实世界的应用中成长起来的,它是由 堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组编写的. 它诞生于 2003 年秋天,那时 Lawrenc ...
- Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen
Android中获取坐标点的一些方法解释 一.getLocationInWindow和getLocationOnScreen的区别 // location [0]--->x坐标,location ...
- PHP FILTER_VALIDATE_IP 过滤器
FILTER_VALIDATE_IP 过滤器把值作为 IP 进行验证,用于验证 IP 是否合法,支持 IPV6 验证 例子 <?php $ip = "192.168.0.1" ...
- js 遍历对象属性(for in、Object.keys、Object.getOwnProperty) 以及高效地输出 js 数组
js中几种遍历对象的方法,包括for in.Object.keys.Object.getOwnProperty,它们在使用场景方面各有不同. for in 主要用于遍历对象的可枚举属性,包括自有属性. ...
- Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...