- 浏览: 183534 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
给定一个二叉树,要求我们将它装换成一个链表的形式。我们从左边开始,假设当前节点为root,我们首先保存root的左孩子leftNode和右孩子rightNode,然后将root的左孩子作为它的右孩
子,然后让root的左孩子为空(root.right = leftNode,root.left = null), 接下来将当然的右孩子也就是root.right(这是新的右孩子,要把这个序列的最后一个节点指向以前的右孩子)指向以前的右孩子rightNode。代码如下:
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
给定一个二叉树,要求我们将它装换成一个链表的形式。我们从左边开始,假设当前节点为root,我们首先保存root的左孩子leftNode和右孩子rightNode,然后将root的左孩子作为它的右孩
子,然后让root的左孩子为空(root.right = leftNode,root.left = null), 接下来将当然的右孩子也就是root.right(这是新的右孩子,要把这个序列的最后一个节点指向以前的右孩子)指向以前的右孩子rightNode。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public void flatten(TreeNode root) { if(root == null) return; flatten(root.left); flatten(root.right); TreeNode leftNode = root.left; TreeNode rightNode = root.right; root.left = null; root.right = leftNode; while(root.right != null) { root = root.right; } root.right = rightNode; } }
发表评论
-
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 497Given 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 429Given 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 580Given 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 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given 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 704For a undirected graph with tre ...
相关推荐
在IT领域,特别是数据结构与算法的学习中,"Flatten Binary Tree to Linked List" 是一个经典的问题。这个任务要求我们将一棵二叉树转换为单链表。二叉树是一种非线性数据结构,而链表则是一种线性数据结构。在实际...
java java_leetcode-114-flatten-binary-tree-to-linked-list
python python_leetcode题解之114_Flatten_Binary_Tree_to_Linked_List
javascript js_leetcode题解之114-flatten-binary-tree-to-linked-list.js
leetcode做过的LeetCode的题目记录一下。对一些比较经典的题型进行分类总结。数据结构 数组 字符串 队列 链表 双指针 栈 堆 树 二叉搜索树 字典树 线段树 并查集 哈希表 图基础... Flatten Binary Tree to Linked List
- **Flatten Binary Tree to Linked List**:将二叉树展平为单链表。 - **Validate Binary Search Tree**:验证一个二叉树是否为二叉搜索树。 - **Recover Binary Search Tree**:恢复二叉搜索树中的两个错误节点...
leetcode 答案leetcode 的工具 这个项目提供了一些工具来更容易地测试 leetcode 答案。 树:切片 <-> TreeNode 此工具有助于将切片转换为 ...--name="Flatten Binary Tree to Linked List" --type=tree
**三、Flatten Binary Tree to Linked List** LeetCode的第114题,要求将二叉树扁平化为单链表,这个问题可以通过分治策略解决。基本步骤如下: 1. **从最简单的case开始**:对于只有一个元素的树,直接返回,即空...
5. **Flatten Binary Tree to Linked List** (二叉树展开为链表): 这是一个关于树的层次遍历的问题,可以使用广度优先搜索(BFS)策略,利用队列(如Java的`Queue`接口)来实现。 6. **Merge Sorted Array** (合并...
flatten_binary_tree_to_linked_list Java 中等的 100.00% 100.00% binary_tree_pruning Java 中等的 100.00% 100.00% insert_into_a_binary_search_tree Java 中等的 100.00% 100.00% maximum_level_sum_of_a_...
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...