二叉树的最小高度,最大高度(深度)和宽度

最大高度

function getMaxHeight(root){
    if(root == null){
        return 0;
    }
    else{
        var left = getMaxHeight(root.left);
        var right = getMaxHeight(root.right);
        return 1 + Math.max(left,right);
    }
}

最小高度

function getMinHeight(root){
    if(root = null){
        return 0;
    }
    else{
        var left = getMinHeight(root.left);
        var right = getMinHeight(root.right);
        return 1 + Math.min(left,right);
    }
}

二叉树宽度

递归方法

function getMaxWidth(root){
    if(root == null){
        return 0;
    }
    else if(root.left == null && root.right == null){
        return 1;
    }
    else{
        return getMaxWidth(root.left) + getMaxWidth(root.right);
    }
}

非递归方法

function getMaxWidth(root){
    if(root == null){return 0}
    var queue = [],
        maxWidth = 1;
    queue.push(root);
    while(true){
        var levelSize = queue.length;
        if(levelSize == 0){
            break;
        }
        while(levelSize > 0){
            var node = queue.shift();
            levelSize--;
            if(node.left){
                queue.push(node.left);
            }
            if(node.right){
                queue.push(node.right);
            }
        }
        maxWidth = Math.max(maxWidth,levelSize);
    }
    return maxWidth;
}
时间: 2024-08-26 05:11:29

二叉树的最小高度,最大高度(深度)和宽度的相关文章

LintCode 二叉树的最小深度

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

求二叉树的最小深度

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

【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. 题目大意 给定

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

题目描述: 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 解题思路: 这个题目比较简单. 对于二叉树的问题,首先想到的是采用递归,广度优先搜索. 一个节点一个节点地遍历,直到第一次找到叶子节点为止. 注意编程的细节,代码里面有注释 参考代码:(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/ 解答 方法一:递归. 这道题可以用递归的方法,一个节点一个节点的把每个节点遍历一遍,并且在遍历的同时记录每个节点相对应的层数, 然后求出叶子节点当中的最小层数就是我们

155 二叉树的最小深度

原题网址:https://www.lintcode.com/problem/minimum-depth-of-binary-tree/description 描述 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题?  是 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4      5 这个二叉树的最小深度为 2 标签 二叉树 Depth-first Search(DFS) 思路:可参照二叉树的

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

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

二叉树的深度和宽度

// 求二叉树的深度和宽度.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include <queue> using namespace std; struct BTNode { char m_value; BTNode *m_left; BTNode *m_right; }; //先序创建二叉树 void CreatBTree(BTNode *&root) { char nV

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest

iOS7中Cell高度 Label高度自适应

? 1 2 3 4 5 6 7 8 9 10 11 12 ?- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{     NSString *str = [_dataArray objectAtIndex:indexPath.row];     UIFont *tfont = [UIFont systemFontOfSize:14.0];     NSDictio