二叉树之 二叉树深度

二叉树深度:
// 获取最大深度
    public static int getMaxDepth(TreeNode root) {
        if (root == null)
            return 0;
        else {
            int left = getMaxDepth(root.left);
            int right = getMaxDepth(root.right);
            return 1 + Math.max(left, right);
        }
    }

二叉树宽度:

使用队列,层次遍历二叉树。在上一层遍历完成后,下一层的所有节点已经放到队列中,此时队列中的元素个数就是下一层的宽度。以此类推,依次遍历下一层即可求出二叉树的最大宽度

// 获取最大宽度
    public static int getMaxWidth(TreeNode root) {
        if (root == null)
            return 0;

        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        int maxWitdth = 1; // 最大宽度
        queue.add(root); // 入队

        while (true) {
            int len = queue.size(); // 当前层的节点个数
            if (len == 0)
                break;
            while (len > 0) {// 如果当前层,还有节点
                TreeNode t = queue.poll();
                len--;
                if (t.left != null)
                    queue.add(t.left); // 下一层节点入队
                if (t.right != null)
                    queue.add(t.right);// 下一层节点入队
            }
            maxWitdth = Math.max(maxWitdth, queue.size());
        }
        return maxWitdth;
    }
时间: 2024-07-30 01:24:34

二叉树之 二叉树深度的相关文章

LintCode 二叉树的最小深度

给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4      5 这个二叉树的最小深度为 2 分析:与最大深度有区别 有单孩子的情况考虑 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * th

二叉树系列 - 二叉树的深度,例 [LeetCode]

二叉树的深度的概念最值得注意的地方,在于 到"叶子"节点的距离. 一般来说,如果直接说“深度”,都是指最大深度,即最远叶子的距离. 这里放两道例题,最小深度和最大深度. 1. 二叉树的最小深度 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest

求二叉树的最小深度

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

创建二叉树 树的深度搜索 广度搜索

树的深度搜索 与树的前序遍历同理 根节点->左孩子->右孩子  树的广度搜索 与树的层次遍历同理 一层一层遍历内容 深度搜索 采用stack的适配器 先进后出原则  而广度搜索采用的queue适配器 先进先出原则 二者正好满足 搜索需求 简要代码如下: #include <iostream> #include <stack> #include <queue> #include <malloc.h> using namespace std; typ

找树节点在二叉树中的深度

/* *pRoot接收要检索的树的根节点,pNode是要确认深度的结点 path存储从根结点到pNode的所有节点,包括了pNode和根节点 */ void findPath(BinaryTreeNode *pRoot, BinaryTreeNode *pNode, vector<int> &path){ if (pRoot == NULL) return; path.push_back(pRoot->m_nValue); if (pRoot == pNode){ //找到了节点

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

[111-Minimum Depth of Binary Tree(二叉树的最小深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目大意 给定

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

给定一个二叉树,获取该二叉树的宽度深度

题目: Description 给定一个二叉树,获取该二叉树的宽度深度. Prototype int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight) Input Param head   须要获取深度的二叉树头结点 Output Param pulWidth   宽度 pulHeight  高度 Return Value 0          成功 1          失败或其它异常 分析

二叉树的最小深度——广度优先搜索

题目描述: 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 解题思路: 这个题目比较简单. 对于二叉树的问题,首先想到的是采用递归,广度优先搜索. 一个节点一个节点地遍历,直到第一次找到叶子节点为止. 注意编程的细节,代码里面有注释 参考代码:(C++) <span style="font-size:18px;">/** * Definition of TreeNode: * class TreeNode { * public: * int

九章算法面试题75 二叉树的最小深度

九章算法官网-原文网址 http://www.jiuzhang.com/problem/76/ 题目 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 在线测试本题 http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/ 解答 方法一:递归. 这道题可以用递归的方法,一个节点一个节点的把每个节点遍历一遍,并且在遍历的同时记录每个节点相对应的层数, 然后求出叶子节点当中的最小层数就是我们