LeetCode OJ-- Populating Next Right Pointers in Each Node
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
二叉树遍历的变种:树的节点多了个next指针。
首先想到肯定和树的遍历有关:先根、中根、后根都不可以。。。
观察找规律,如果当前节点是它根的 left,则它的next就是它根的 right。
如果当前节点是它根的right,则它的next 就是 它根的next的left,如果它根的 next 存在的话。
- /**
- * 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;
- root->next = NULL;
- visit(root);
- }
- void visit(TreeLinkNode *root)
- {
- if(root->left)
- {
- root->left->next = root->right;
- root->right->next = (root->next ? root->next->left:NULL);
- visit(root->left);
- visit(root->right);
- }
- }
- };
LeetCode OJ-- Populating Next Right Pointers in Each Node的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 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 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- Java for LeetCode 117 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] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【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 I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- Flow Problem HDU - 3549
Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...
- loj2173 「FJOI2016」建筑师
ref 真是道组合数学神题啊--第一次见第一类斯特林数-- #include <iostream> #include <cstdio> using namespace std; ...
- IOS开发学习笔记028-UITableView单组数据显示代码优化
1.如果表格中又几百条数据的话,系统会自动加载显示在界面上得数据,逐一加载 添加100个数据到UITableView中 ; i < ; i ++) { NSString *icon = [NSS ...
- 无法启动此程序,因为计算机中丢失OgreMain_d.dll。尝试重新安装该程序以解决此问题。
这个问题很奇怪啊,不明白什么原因? 打开VS2010,打开项目,运行,就提示”无法启动此程序,因为计算机中丢失OgreMain_d.dll.尝试重新安装该程序以解决此问题.“ 然后就去配置环境变量,包 ...
- 从shell(终端)中退出python
从shell(终端)中退出python: 1.输入命令行:$ exit() 2.快捷键: ctrl+Z
- mvc-自定义视图引擎
//自定义视图引擎的实质是把数据模型(moudle)和模板(View)转换成html页面,输出到客户端public class MyView:IView { string _viewPath; pub ...
- [oldboy-django][2深入django]Form组件实现生成: select下拉框, checkbox复选框,radio单选框以及如何实现自定义数据格式要求
1 需求 - 1Form组件如何实现生成选择类标签: select,check, radio - 默认值 - 保留上次输入的值 - 2自定义验证规则 - RegexField - -
- CodeForces839B[思维] Codeforces Round #428 (Div. 2)
#include <bits/stdc++.h> using namespace std; int n, k; ; ], cnt[]; void solve() { int t; cnt[ ...
- hihoCoder #1783 又一个重复计数
题目大意 给定一个长度为 $n$ 的字符串 $S$,定义函数 $f(S)$ 表示 $S$ 的不同回文子串的个数.对于 $1\le l \le r \le n$,定义 $S[l,r]$ 为字符串 $S$ ...
- [luogu1357] 花园 [dp+矩阵快速幂]
题面: 传送门 思路: 把P形花圃记录为0,C形记录为1,那么一段花圃就可以状态压缩成一个整数 那么,我们可以有这样的状压dp: dp[i][S]表示前i个花圃,最后m个的状态为S的情况 如果这是一条 ...