`
淡淡的一抹
  • 浏览: 19309 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.


1 自己的代码

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) { val = x; }
	}

public class MaximumDepthOfBinaryTree {
	public int maxDepth(TreeNode root) {
		if(root == null) return 0;//不要忘记这个,不然会出现空指针异常
		
		int depth = 0;
		int leftDepth = 0;
		int rightDepth = 0;
		if(root.left != null) leftDepth = maxDepth(root.left);
		if(root.right != null) rightDepth = maxDepth(root.right);
		if(leftDepth >= rightDepth) depth = leftDepth;
		else depth = rightDepth;
        return depth + 1;
    }
	
	}
分享到:
评论

相关推荐

    cpp-算法精粹

    Maximum Depth of Binary Tree Path Sum Path Sum II Binary Tree Maximum Path Sum Populating Next Right Pointers in Each Node Sum Root to Leaf Numbers LCA of Binary Tree 线段树 Range Sum Query - Mutable ...

    javalruleetcode-what_the_dead_men_say:what_the_dead_men_say

    java lru leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com... 二叉树 0098 Validate Binary ...Tree ...Binary ...Tree ...tree ...Binary Tree ...Binary Tree ...Maximum depth of binary tree - Java Iterative

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 ...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷...Depth of Binary Tree 【递归】 遍历求单个数字 136. Single Number 石头游戏 292. Nim Gam

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by ...104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。...Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]

    leetcode答案-leetcode:leetcode

    Maximum Depth of Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、肉眼检查,提交的准确率!借助线下debug工具,有何难度可言?丝毫没有模拟在线...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a ...com.leetcode.tree ...Binary Tree Maximum Depth of Binary Tree Same Tree

    JJ.rar_names

    One names depth of a tree the maximum number of "descents" which can be carried out starting from the root. For example, the third binary tree of figure 1 has a depth of 3.According to this reasoning,...

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-...

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    [104_maximum-depth-of-binary-tree.cpp] [105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-...

    leetcode跳跃-Algorithm:算法学习,包括leetcode算法题,

    maximum-depth-of-binary-tree 105 construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-...

    Computing and Combinatorics

    Imbalance Is Fixed Parameter Tractable.- The Ramsey Number for a Linear Forest versus Two Identical Copies of Complete Graphs.- Computational Geometry.- Optimal Binary Space Partitions in the Plane.-...

    算法导论_英文第三版

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 ? 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 ...

    数据结构常用算法c++实现

    Binary search tree Dynamic order statistics Red-black tree Interval tree Prefix Tree(Trie) *Suffix Tree(未实现)* B-Tree Hash by multiplication Hash table Universal hash function Perfect hash Java's ...

    Introduction to Algorithms, 3rd edtion

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3...

    算法导论--Introduction.to.Algorithms

    The book covers a broad range of algorithms in depth, yet makes their design and analysis accessible to all levels of readers. Each chapter is relatively self-contained and can be used as a unit of ...

    算法导论 第三版 英文原版 高清文字版

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 ? 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 ...

Global site tag (gtag.js) - Google Analytics