leetcode之Maximum Depth of Binary Tree 以及Balanced 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 le = maxDepth(root.left);
		int ri = maxDepth(root.right);
		return le > ri ? (le+1) : (ri+1);
    }

  扩展:判断一棵树是不是平衡二叉树?

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

可以使用一般的方法,对每个结点求出左右深度,然后深度相减,绝对值是否小于1,但是这样会可能重复遍历一个节点多次。

代码一:虽然有重复计算,但是最终返回值那里很精妙。

    public boolean isBalanced(TreeNode root) {

	        return maxDepth1(root) != -1;
	    }

	private static int maxDepth1(TreeNode root) {
	        if (root == null) {
	            return 0;
	        }

	        int left = maxDepth1(root.left);
	        int right = maxDepth1(root.right);
	        if (left == -1 || right == -1 || Math.abs(left-right) > 1) {
	            return -1;
	        }
	        return Math.max(left, right) + 1;

    }

  代码2:利用了TreeNode结构中的val,用它来记录以当前结点为根的子树的高度,避免多次计算。

  public boolean isBalanced(TreeNode root) {
	height(root);
        return run(root);
    }  

    public boolean run(TreeNode root) {
        if (root == null) return true;  

        int l = 0, r = 0;
        if (root.left != null) l = root.left.val;
        if (root.right != null) r = root.right.val;
        if (Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right)) return true;  

        return false;
    }  

    public int height(TreeNode root) {
        if (root == null) return 0;
        root.val = Math.max( height(root.left), height(root.right) ) + 1;
        return root.val;
    }

  

时间: 2024-10-05 08:36:30

leetcode之Maximum Depth of Binary Tree 以及Balanced Binary Tree的相关文章

【LeetCode】 Maximum Depth of Binary Tree

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. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.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. class Solution { public: int maxDepth(TreeNode *root) { inM

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][Java]Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/ 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

[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. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

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

[LeetCode] 104. Maximum Depth of Binary Tree 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. 题意及分析:找出一棵树的高度,即最深子节点.使用深度遍历的方法即可,用一个变量记录遍历到当前点的最大高度,然后当前点若有子节点,遍历到子节点,那么该点的高度+1和当前的最大

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

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和BFS方法. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNod