- 浏览: 183714 次
- 性别:
- 来自: 济南
文章分类
最新评论
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
给定一个完全二叉树,多了一个*next变量,next指向紧邻当前节点右边的节点。我们可以用队列完成,一层一层的处理,也可以用递归完成。代码如下:
1,迭代
2,递归
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
给定一个完全二叉树,多了一个*next变量,next指向紧邻当前节点右边的节点。我们可以用队列完成,一层一层的处理,也可以用递归完成。代码如下:
1,迭代
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public void connect(TreeLinkNode root) { Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); if(root == null) return; queue.offer(root); int count = 0; int helper = 1; TreeLinkNode pre = null; while(!queue.isEmpty()) { TreeLinkNode node = queue.poll(); helper --; if(pre != null){ pre.next = node; } pre = node; if(node.left != null) { queue.offer(node.left); count ++; } if(node.right != null) { queue.offer(node.right); count ++; } if(helper == 0) { pre.next = null; helper = count; count = 0; pre = null; } } } }
2,递归
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public void connect(TreeLinkNode root) { if(root == null) return; if(root.left != null) { root.left.next = root.right; if(root.next != null) { root.right.next = root.next.left; } } connect(root.left); connect(root.right); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 707For a undirected graph with tre ...
相关推荐
java基础 java_leetcode题解之Populating Next Right Pointers in Each Node
java java_leetcode题解之Populating Next Right Pointers in Each Node.java
python python_leetcode题解116_Populating_Next_Right_Pointers_in_Each_Node
javascript js_leetcode题解之116-populating-next-right-pointers-in-each-node.js
Populating Next Right Pointers in Each Node I && II) PIE (未录入) CC150 (未录入) EPI (未录入) 每一个题库对应problems路径下的一个文件夹,每一个题目对应相应题库下的一个Python文件。每一个题目都要至少实现...
leetcode卡 leetcode_python ...Populating Next Right Pointers in Each Node 2020-01-29 783 Minimum Distance between bst nodes 2020-01-30 173 二叉搜索树迭代器 2020-01-31 230 二叉搜索树中
- **Populating Next Right Pointers in Each Node**:连接二叉树每个节点的下一个右侧节点。 - **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:...
力扣第七题通常是指“重建二叉树”(重建二叉树的英文原题可能是“Populating Next Right Pointers in Each Node”)。这是一道中等难度的题目,主要考察的是树结构和深度优先搜索(DFS)或广度优先搜索(BFS)的...
[117]填充每个节点的下一个右侧节点指针 II|populating-next-right-pointers-in-each-node-ii给定一个二叉树填充
四平方和定理 leetcode Leetcode practice Table of content Tree 92.reverse-linked-list-ii (反转链表 ...94.binary-tree-inorder-...116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的题目, 分为一些子项 bytedance ...populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
蓄水池算法 leetcode leetcode Post: 《双指针的魅力》 《常见面试题思想方法整理》 ...populating-next-right-pointers-in-each-node-ii: 二级指针代码虽然简洁优雅,但是对性能有影响,不如一级指针加if else判断快。