leetcode104,111,543 求二叉树深度的dfs解法

104.求二叉树的最大深度

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None:
            return 0
        else:
            leftdepth = self.maxDepth(root.left)
            rightdepth = self.maxDepth(root.right)
            return max(leftdepth, rightdepth) + 1 #!!!关键在于+1

111.求二叉树的最小深度

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        if not root.left:
            return self.minDepth(root.right)+1
        if not root.right:
            return self.minDepth(root.left)+1
        return min(self.minDepth(root.left), self.minDepth(root.right))+1

543.求二叉树的直径

class Solution:
    def diameterOfBinaryTree(self, root: TreeNode) -> int:
        self.ans = 1
        def depth(node):
            if not node: return 0
            L = depth(node.left)
            R = depth(node.right)
            self.ans = max(self.ans, L+R+1)
            return max(L, R) + 1
        depth(root)
        return self.ans - 1

原文地址:https://www.cnblogs.com/yawenw/p/12683921.html

时间: 2025-01-02 07:28:01

leetcode104,111,543 求二叉树深度的dfs解法的相关文章

求二叉树深度

概念: 1.二叉树深度:树中结点的最大层次称为树的深度或高度. 2.二叉树层次:从根开始定义起,根为第一层,根的孩子为第二层,以此类推. 要点: 1.递归. 2.二叉树深度为左右子树深度较大值+1. 代码: /* 求二叉树深度 by Rowandjj 2014/7/13 ------------------------------- 题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 输入: 第一行输入有n,n表示

二叉树(4)----求二叉树深度

1.二叉树定义 typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树深度 定义:对任意一个子树的根节点来说,它的深度=左右子树

求二叉树深度和copy二叉树

// operatorTree.cpp // 对树的操作 #include <iostream> #include <cstdio> // 二叉树表示法 typedef struct BiTNode { int data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; // 中序遍历 void inOrder(BiTNode *T) { if (T == NULL) { return; } inOrder(T->lch

[数据结构]求二叉树的深度与宽度

二叉树数据结构声明: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; 1.递归求二叉树深度 int getDepth(TreeNode *root) { if (root == NULL) { return 0; } return getDepth(root->left) > getDepth(root->right) ?(getDepth(root->left) + 1) : (getDepth(roo

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: 1 typedef struct TreeNode{ 2 int data; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 }TreeNode; 2.创建根节点: 1 TreeNode *creatRoot(){ 2 TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode)); 3 if(NULL=

求二叉树的最小深度

思路:用递归的方法求解. 输入:二叉树的根节点: 输出:二叉树的最小深度. 最小深度的定义:从根节点到叶子节点的最短路径上的节点数. 算法如下: 将二叉树分为这么几种情况: 传入的根节点为空,返回NULL: 传入根节点不为空,左子树为空,右子树为空,返回最小深度1: 传入根节点不为空,左子树为空,右子树不为空,返回右子树的最小深度+1: 传入根节点不为空,左子树不为空,右子树为空,返回左子树的最小深度+1: 传入根节点不为空,左右子树都不为空,则返回左右子树中最小深度的较小值+1. 代码如下:

二叉树深度、平衡、相等、求遍历

#include <iostream> #include <fstream> #include <string> using namespace std; struct TreeNode { struct TreeNode* left; struct TreeNode* right; char elem; }; //知道先序遍历和中序遍历 求后序遍历 TreeNode* BinaryTreeFromOrderings(char* inorder, char* preor

求二叉树的深度代码实现

用递归的方案实现: 1 /* 求二叉树的深度 */ 2 int getDepthBiTree(BITREENODE* T) 3 { 4 int lDepth = 0, rDepth = 0; 5 6 if(T == NULL) 7 { 8 return 0; 9 } 10 else 11 { 12 lDepth = getDepthBiTree(T->lchild); 13 rDepth = getDepthBiTree(T->rchild); 14 } 15 16 return lDepth

[华为机试练习题]42.求二叉树的深度和宽度

题目 题目标题: 求二叉树的宽度和深度 给定一个二叉树,获取该二叉树的宽度和深度. 例如输入 a / b c / \ / d e f g 返回3. 接口说明 原型: int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight) 输入参数: head 需要获取深度的二叉树头结点 输出参数(指针指向的内存区域保证有效): pulWidth 宽度 pulHeight 高度 返回值: 0 成功 1 失败或