LeetCode - Medium - 114. Flatten Binary Tree to Linked List
Topic
- Tree
- Depth-first Search
Description
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
Given the root
of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode
class where the right
child pointer points to the next node in the list and the left
child pointer is always null
.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [0]
Output: [0]
Constraints:
- The number of nodes in the tree is in the range
[0, 2000]
. -100 <= Node.val <= 100
Follow up: Can you flatten the tree in-place (with O(1)
extra space)?
Analysis
方法一:我写的,用到二叉树前序遍历模式的递归法。
方法二:别人写的,很简洁。
方法三:别人写的,跟方法一的核心思想类似,区别在于本方法只用单一方法就搞定。
方法四:别人写的,迭代法。
Submission
package com.lun.medium;import java.util.LinkedList;import com.lun.util.BinaryTree.TreeNode;public class FlattenBinaryTreeToLinkedList {//方法一:我写的,用到二叉树前序遍历模式的递归法public void flatten(TreeNode root) {flattenHelper(root);}private TreeNode flattenHelper(TreeNode node) {if(node == null)return null;TreeNode left = node.left; TreeNode right = node.right;node.left = null;node.right = flattenHelper(left);TreeNode p = node;while(p.right != null)p = p.right;p.right = flattenHelper(right); return node;}//方法二:别人写的,很简洁private TreeNode prev = null;public void flatten2(TreeNode root) {if (root == null)return;flatten2(root.right);flatten2(root.left);root.right = prev;root.left = null;prev = root;}public void setPrevNull() {this.prev = null;}//方法三:别人写的,跟方法一的核心思想类似,区别在于本方法只用单一方法就搞定public void flatten3(TreeNode root) {if (root == null) return;TreeNode left = root.left;TreeNode right = root.right;root.left = null;flatten(left);flatten(right);root.right = left;TreeNode cur = root;while (cur.right != null) cur = cur.right;cur.right = right;}//方法四:别人写的,迭代法public void flatten4(TreeNode root) {if (root == null) return;LinkedList<TreeNode> stk = new LinkedList<>();stk.push(root);while (!stk.isEmpty()){TreeNode curr = stk.pop();if (curr.right!=null) stk.push(curr.right);if (curr.left!=null) stk.push(curr.left);if (!stk.isEmpty()) curr.right = stk.peek();curr.left = null; // dont forget this!! }}}
Test
import static org.junit.Assert.*;
import org.junit.Test;import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;public class FlattenBinaryTreeToLinkedListTest {@Testpublic void test() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test2() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten2(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));obj.setPrevNull();TreeNode root2 = null;obj.flatten2(root2);assertNull(root2);obj.setPrevNull();TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten2(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test3() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten3(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten3(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten3(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test4() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten4(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten4(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten4(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}
}
LeetCode - Medium - 114. Flatten Binary Tree to Linked List相关推荐
- Leetcode:114. Flatten Binary Tree to Linked List
题目 Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: ...
- Java for LeetCode 114 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1/ \2 5/ \ \3 4 6 The ...
- leetcode [114]Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...
- 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目 Given a binary tree, flatten it to a linked list in-place. For example, Given 1/ \2 5/ \ \3 4 6 T ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】...
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...
- 114. Flatten Binary Tree to Linked List 二叉树展开为链表
给定一个二叉树,原地将它展开为一个单链表. 例如,给定二叉树 1/ \2 5/ \ \ 3 4 6 将其展开为: 1\2\3\4\5\6 前序遍历 将二叉树展开为单链表之后,单链表中的节点顺序即为二叉 ...
- LeetCode: Flatten Binary Tree to Linked List
LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...
- Flatten Binary Tree to Linked List - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Flatten Binary Tree to Linked List - LeetCode 注意点 不要访问空结点 val会有负值 解法 解法一:递归,D ...
- [Leetcode] Flatten Binary Tree to Linked List 整平二叉树
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
最新文章
- 老板:kill -9的原理都不知道就敢到线上执行,明天不用来了
- RESTful API 设计指南(转)
- Android自动化测试之monkeyrunner基本要素(七)
- 极其实用的sql脚本【建议收藏】
- 前端学习(2029)vue之电商管理系统电商系统之timeline组件
- 【转】C++中如何区分构造函数与重载operator()得到的仿函数?
- php安装mem+cache扩展,安装memcached及php扩展
- 桌面环境选择_Ubuntu 18.04 桌面环境初体验
- centos 调整home分区xfs_centos下扩容根分区(针对xfs和ext4不同文件系统)
- 如何在iPhone上安装Skype?
- python用于cad_python cad
- python星号直角三角形边长公式_三角形边长计算公式大全
- linux 命令系列之 history(41)
- 数据分析/运营——数据异常的排查方法
- 万豪国际亚太区第1000家酒店开业!总客房数量超一半在中国 | 美通社头条
- python123用户登录c_写代码: 实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败。...
- 如何消除原生Android,如何消除原生Android网络状态上的惊叹号
- 苹果xr十大隐藏功能_网上找的一些非常实用的苹果笔记本使用技巧及隐藏功能,收藏了...
- idea设置代码提示
- WordPress让插件随主题一同启用
热门文章
- python alter table_python(pymysql)之mysql简单操作
- 单选按钮_PerlTk教程之按钮Button、复选按钮Checkbutton、单选按钮Radiobutton(附完整代码)...
- arcgis中的python字符串比较
- 【转】卷积的本质及物理意义(全面理解卷积)
- 【转】c# 操作webservice(经典入门教程+MSDN必胜)(有自己修改的部分)
- 【转】Dynamics 365 CRM 开发架构简介
- Execute SQL Task 参数和变量的映射
- sqlserver连接字符串_10分钟使用EF Core连接MSSQL数据库
- linux怎么检测文件完整性,Linux如何基于AIDE检测文件系统完整性
- 【HDU - 6567】Cotree(树形dp,思维)