DepthBinaryTree leetcode java python
来源:互联网 发布:windows设备管理器在哪 编辑:程序博客网 时间:2024/05/28 23:11
#coding:utf-8class TreeNode: def __init__(self,val,left,right): self.val=val self.left=left self.right=rightclass Solution: def reslove(self,root): ans=[] level=[root] numlevel = 0 while level and root: subans=[] sublevel=[] for i in range(len(level)): if level[i].left is not None: sublevel.append(level[i].left) if level[i].right is not None: sublevel.append(level[i].right) subans.append(level[i].val) level=sublevel ans.append(subans) numlevel=len(ans) return numlevelif __name__=="__main__": #you write the root
java:class TreeNode{ int val; TreeNode left=null; TreeNode right=null; void TreeNode(int val){ this.val=val; }}public class DepthBinaryTree{ public int depthsolution(TreeNode root){ List<List<Integer>> list=new ArrayList<List<Integer>>(); Queue<TreeNode> queue=new LinkedList<TreeNode>(); int numlevel=0; if(root==null) return null; queue.offer(root); while(!queue.isEmpty){ List<Integer> subList=new ArrayList<Integer>(); int queuesize=queue.size(); for(int i=0;i<queuesize;i++){ if(queue.peer().left!=null){ queue.offer(queue.peer().left); } if(queue.peer().right!=null){ queue.offer(queue.peer().right); } sublist.add(queue.poll().val); } list.add(sublist); } numlevel=list.length; return numlevel; }}
0 0
- DepthBinaryTree leetcode java python
- leetcode:SameTree java python
- leetcode BinaryTreeLevel java python
- BinarySortTree java python leetcode
- Leetcode FlattenBinaryTree Java Python
- 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
- 【JQuery】用JQuery来监听浏览器改变窗口大小事件
- hdu4791 Labyrinth
- Hive java编程提交查询语句和配置
- 窗体的生成
- SQL学习笔记8——查询到数据库的值为null在C#中如何判断
- DepthBinaryTree leetcode java python
- Hbase 学习笔记一 》starting from scrath
- Android USER 版本与ENG 版本的差异--MTK官方解释
- Qt 常用类 (8)—— QIcon
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 常用排序算法----Java实现
- for-in switch
- android 抛出Error:Execution failed for task .....
- IOS 二维码扫码