Maximum Depth of Binary Tree-求二叉树高度

非递归: public static int maxDepth(TreeNode root){
        if(root==null) return 0;
        Queue<TreeNode> q=new LinkedList<TreeNode>();
        q.offer(root);
        int enQueNum=1;
        int visitedNum=0;
        int lastLevNum=1; //做标记 标记到每层最后的节点 当visitedNum与它相等时本层结束
        int height=0;

        while(!q.isEmpty()){
            TreeNode now=q.poll();
            visitedNum++;
            if(now.left!=null){
                q.offer(now.left);
                enQueNum++;
            }
            if(now.right!=null){
                q.offer(now.right);
                enQueNum++;
            }
            if(visitedNum==lastLevNum){
                height++;
                lastLevNum=enQueNum;//lastLevNum的值赋为此时最后进队列的节点数值也是本层的最后节点
            }
        }
         return height;
     }

递归:

public static int maxDepth2(TreeNode root){
            if(root==null) return 0;
            int leftHeight=0;
            int rightHeight=0;
            leftHeight=maxDepth2(root.left);
            rightHeight=maxDepth2(root.right);
            return leftHeight>rightHeight?leftHeight+1:rightHeight+1;
     }
时间: 2024-12-10 14:16:43

Maximum Depth of Binary Tree-求二叉树高度的相关文章

Leetcode 104. 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)如果根节点是空,则返回0:否则转到(2) (2)  l = 左子树的最大深度; r = 右子树的最大深

LeetCode 104. 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. 题目标签:Tree 这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量.利用post order 来遍历二叉树,对于每一个点,它的两个chi

leetCode 104.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. 思路:很基础的一个题,DFS即可.代码如下: /** * Definition for a binary tree node. * public class TreeNode {

LeetCode Maximum Depth of Binary Tree (求树的深度)

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 *

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

[LintCode] 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. Have you met this question in a real interview? Yes Example Given a binary tree as follow:

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

[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 给定

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

LeetCode——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. 此题和求二叉树的最短路径几乎一模一样. public int maxDepth(TreeNode root) { if (root == null) return 0; int a

学习笔记:LeetCode104:Maximum Depth of Binary Tree

LeetCode104: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 /** 2 * De