[Leetcode 104]求二叉树的深度Depth of BinaryTree
【题目】
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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
【思路】
递归,1+Math.max(maxDepth(root.left),maxDepth(root.right));
【解答】
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
【其他定义】
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; return 1+Math.max(maxDepth(root.left),maxDepth(root.right)); } }
[Leetcode 104]求二叉树的深度Depth of BinaryTree的更多相关文章
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...
- PTA 求二叉树的深度
6-7 求二叉树的深度 (6 分) 本题要求实现一个函数,可返回二叉树的深度. 函数接口定义: int Depth(BiTree T); T是二叉树树根指针,函数Depth返回二叉树的深度,若树为 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- php求二叉树的深度(1、二叉树就可以递归,因为结构和子结构太相似)(2、谋而后动,算法想清楚,很好过的)
php求二叉树的深度(1.二叉树就可以递归,因为结构和子结构太相似)(2.谋而后动,算法想清楚,很好过的) 一.总结 1.二叉树就可以递归,因为结构和子结构太相似 2.谋而后动,算法想清楚,很好过的 ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 求二叉树的深度,从根节点到叶子节点的最大值,以及最大路径(python代码实现)
首先定义一个节点类,包含三个成员变量,分别是节点值,左指针,右指针,如下代码所示: class Node(object): def __init__(self, value): self.value ...
- 求二叉树的深度 python
二叉树有深度和高度两个属性,一个节点的深度指的是从根节点到该节点路径的长度,根节点的深度为1:一个节点的高度指的是从该节点到叶子节点所有路径上包含节点个数的最大值.叶子节点的高度为1,往上节点的高度依 ...
- 【剑指offer】输入一颗二叉树的根节点,求二叉树的深度,C++实现
原创博文,转载请注明出处! # 题目 # 举例 下图二叉树的深度为4,最长路径为1-2-5-7. # 思路(递归) 如果一个树只有一个节点,它的深度为1: 如果根节点只有左子 ...
- LeetCode111_求二叉树最小深度(二叉树问题)
题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...
随机推荐
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- caffe---mnist数据集训练与测试
1.数据.mnist_test_lmdb和mnist_train_lmdb数据 2.路径. (1)修改lenet_train_test.prototxt文件,训练和测试两处 source: " ...
- Vue音乐项目笔记(五)
1.搜索列表的点击删除.删除全部的交互事件 https://blog.csdn.net/weixin_40814356/article/details/80496097 seach组件中放search ...
- Vue音乐项目笔记(二)
1. Vuex https://blog.csdn.net/weixin_40814356/article/details/80347366 编写: 然后,在main.js中引入 在组件中改变stat ...
- A Chess Game POJ - 2425
Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesse ...
- Tree总结
树结构问题因为容易写出解法,因此经常出现在面试题中 1. 树的种类 1) Tree 2) Binary Trees 3) Binary Search Trees(BST) : used to sort ...
- 第三周学习进度条+PSP0过程文档
第三周学习进度条 第三周 所花时间(包括上课) 14:30-15:35(65)+19:00-21:20(140)+17:52-19:00(68)+19:10-20:45(95)+21:00-22 ...
- 5月16 JSON的一些知识点及AJAX的应用
什么是JSON? JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,(文本数据交换格式).JSON简单说就是javascript中的对象和数组, ...
- stl常用的查找算法
#include<iostream> using namespace std; #include"vector" #include"algorithm&quo ...
- JS 设置盒子div 跳转
方式一 window.location.href=”url”; 在当前窗口跳转 方式二 window.open(‘url’) 在新窗口跳转 window.open(‘url’,’_self’) 在当前 ...