Minimum Depth of Binary Tree
来源:互联网 发布:男朋友很厉害体验知乎 编辑:程序博客网 时间:2024/06/16 04:25
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDepth(TreeNode root) { int res = 0;if (root == null) {return res;}if(root.left == null && root.right == null){return 1;}ArrayList<TreeNode> tmp = new ArrayList<TreeNode>();tmp.add(root);boolean finded = false;boolean findedLeft = false;boolean findedRight = false;while(tmp.size() != 0){ArrayList<TreeNode> next = new ArrayList<TreeNode>();for(int i = 0 ; i < tmp.size() ; i++){if(tmp.get(i).left != null){next.add(tmp.get(i).left);findedLeft = false;}else{findedLeft = true;}if(tmp.get(i).right != null){next.add(tmp.get(i).right);findedRight = false;}else{findedRight = true;}if(findedLeft && findedRight){finded = true;break;}}res++;if(finded){break;}tmp = next;}return res < 2 ? 2 : res; }}理解了这个概念,改了好多次
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- 当GOLang遇到大量ESTABLISHED时
- 安全、兼容性及权限——关于UAC的一席谈(略更新)
- C# SSH Shell终端模拟控件Rebex SSH Shell详细介绍
- STM8S PWM 应用 呼吸灯
- FregServer进程,启动Binder线程池,睡眠等待在proc->wait
- Minimum Depth of Binary Tree
- js 采用call实现继承
- U-Boot启动过程--详细版的完全分析 .
- grep
- 解析C语言结构体对齐(内存对齐问题)
- 多tomcat启动
- centos apache httpd.conf文件初始化配置
- 苹果收购Beats,花32亿美元为的是苹果电视
- Android 启动文件