/**
*
* Source : https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/
*
*
*
* Given a binary tree, find its maximum depth.
*
* The maximum depth is the number of nodes along the longest path from the root node
* down to the farthest leaf node.
*/
public class MaxDepthOfBinaryTree { /**
* 求出二叉树树的深度,使用深度优先
*
* @param root
* @return
*/
public int maxDepth (TreeNode root) {
if (root == null) {
return 0;
}
int leftMax = 1;
int rightMax = 1;
leftMax += maxDepth(root.leftChild);
rightMax += maxDepth(root.rightChild); return Math.max(leftMax, rightMax);
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
MaxDepthOfBinaryTree depthOfBinaryTree = new MaxDepthOfBinaryTree();
char[] arr = new char[]{'3','9','2','#','#','1','7'}; System.out.println(depthOfBinaryTree.maxDepth(depthOfBinaryTree.createTree(arr)));
}
}

leetcode — maximum-depth-of-binary-tree的更多相关文章

  1. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. Leetcode Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

  7. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  9. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  10. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

随机推荐

  1. CentOS7上安装Snipe-IT4.6.3详细过程及注意事项

    笔者采用的是CentOS7,先对系统进行Update,然后安装军哥的LNMPA,详情请参考lnmp.org 注意:安装LNMPA前需要修改lnmp.conf中这一行为下面,也就是要安装PHP的File ...

  2. C\C++ 内存对齐现象

    前几天一个在自学C语言的小伙伴问了我个问题,C语言结构体储存所占空间为啥和自己预测的不一样.看一下下面这一段代码: struct node{ int num; char ch; }a; printf( ...

  3. Linux指令集

    最近在学习Linux虚拟机,下面是我在学习虚拟机的过程中使用过的一些指令,及其作用. pwd-> 列出当前目录路径 ls-> 列出当前目录列表 cd-> 改变目录 mkdir-> ...

  4. 爬虫下载City Scape数据

    爬虫下载City Scape数据 CityScape是道路场景的经典数据集,但是如right Img8bit_sequence_trainvaltest达到322G,需要用服务器下载比较方便. 需求场 ...

  5. ubuntu/deepin 下 Sha 哈 dow 哈 socks 全局配置

    1. 安装编译环境 Ubuntu在默认情况下没有提供C/C++的编译环境,因此需要手动安装.如果单独安装gcc以及g++比较麻烦,为了方便,我们直接安装Ubuntu提供的build-essential ...

  6. RDSS和RNSS

    RNSS英文全称Radio Navigation Satellite System,由用户接收卫星无线电导航信号,是一种卫星无线电导航业务,自主完成至少到4颗卫星的距离测量,进行用户位置,速度及航行参 ...

  7. 用Java写hello world

    public class HelloWorld{ public static void main(String[] args){ System.out.println("hello worl ...

  8. C# 使用NPOI 处理Excel(Datable与Excel相互转换)

    VS上有自带的程序集可以读取,但是总是会出现这样或那样的问题,让人恨得牙疼!而且效率太慢了.用NPOI就好多了,比较快,而且稳定,还简单,引用相应的程序集就好了. Excel转换成Datable pr ...

  9. 简单工厂模式demo

    1. 简单工厂模式 domain的接口 public interface Color{ public void display(); } red public Class Red implements ...

  10. 两行 CSS 代码实现图片任意颜色赋色技术

    很久之前在张鑫旭大大的博客看到过一篇 PNG格式小图标的CSS任意颜色赋色技术,当时惊为天人,感慨还可以这样玩,私底下也曾多次想过有没有其他方法可以实现,又或者不仅仅局限于 PNG 图片. mix-b ...