Leetcode FlattenBinaryTree Java Python
来源:互联网 发布:unity3d序列帧动画 编辑:程序博客网 时间:2024/06/11 11:20
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
JAVA:
Java:class TreeNode{ int val; TreeNode left=null; TreeNode right=null; void TreeNode(int val){ this.val=val; }} public class FlattenBinaryTree{ public TreeNode reslove(TreeNode root){ int[] ans; dfs(root,ans); build(ans); return ans[0]; } public void dfs(TreeNode root,int[] ans){ if(root==null){ return; } ans.append(root); dfs(root.left,ans); dfs(root.right,ans); } public void build(int[] ans){ ans.add(null); for(int i;i<ans.length-2,i++){ ans[i].left=null; ans[i].right=ans[i+1]; } }}Python:
#coding:utf-8class TreeNode: def __init__(self,val,left,right): self.val=val self.left=left self.right=rightclass FlattenBinaryTree: def reslove(self,TreeNode root): ans=[] dfs(root,ans); Build(ans); return ans[0]; def dfs(TreeNode root,int[] ans){ if not root: return ans.append(root) dfs(root.left,ans) dfs(root.right,ans) } def Build(self,ans): ans.append(None) for i in range(len(ans)-1): ans[i].left=None ans[i].right=ans[i+1]
0 0
- Leetcode FlattenBinaryTree Java Python
- leetcode:SameTree java python
- leetcode BinaryTreeLevel java python
- DepthBinaryTree leetcode java python
- BinarySortTree java python leetcode
- Two Sum Leetcode Python Java
- Reverse Integer Leetcode Python Java
- Palindrome Number Leetcode Python Java
- 3Sum Leetcode Python Java
- LeetCode--Reverse Integer (Cpp,Java,Python)
- Leetcode path-sum-ii Python Java
- Pascal's Triangle Leetcode Python Java
- Pascal's Triangle II Leetcode Python java
- Add Two Sum Leetcode Python java
- Longest Palindromic Substring Leetcode Python Java
- Container With Most Water Leetcode Python Java
- Longest Common Prefix Leetcode Python Java
- 3Sum Closest Leetcode Python Java
- 使用wsimport命令生成webService客户端代码实例
- LRSD: Low Rank and Sparse matrix Decomposition
- 博客数破100记录
- 数据库存储I/O类型分析与配置
- ASP.Net请求处理机制初步探索之旅 - Part 3 管道
- Leetcode FlattenBinaryTree Java Python
- Leetcode自我做题总结汇总(持续更新)
- [Q学习]11 Qt事件系统——过滤事件
- 跳跃的游戏 计蒜客 ------ACM随笔
- ASP.Net请求处理机制初步探索之旅 - Part 4 WebForm页面生命周期
- [leetcode]33. Search in Rotated Sorted Array
- 在GitHub上建立自己在线简历
- ASP.Net请求处理机制初步探索之旅 - Part 5 ASP.Net MVC请求处理流程
- Java千百问_08JDK详解(009)_java程序如何运行