码迷,mamicode.com
首页 > 其他好文 > 详细

33. Minimum Depth of Binary Tree && Balanced Binary Tree

时间:2014-08-27 18:25:08      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   for   ar   art   div   

Minimum Depth of Binary Tree

OJ: https://oj.leetcode.com/problems/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.

思想:先序遍历。注意的是: 当只有一个孩子结点时,深度是此孩子结点深度加 1 .

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
       if(root == NULL) return 0;
       if(root->left == NULL && root->right == NULL) return 1;
       int l = minDepth(root->left);
       int r = minDepth(root->right);
       return (l && r) ? min(l, r) + 1 : (l+r+1);
    }
};

Balanced Binary Tree

OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/

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.

 思想: 先序遍历。既要返回左右子树判断的结果,又要返回左右子树的深度进行再判断。

          所以要么返回一个 pair<bool, int>, 要么函数参数增加一个引用来传递返回值。

方法1:返回一个 pair<bool, int>: (更简洁)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
typedef pair<bool, int> Pair;
Pair judge(TreeNode *root) {
    if(root == NULL) return Pair(true, 0);
    Pair L = judge(root->left);
    Pair R = judge(root->right);
    return Pair(L.first && R.first && abs(L.second-R.second) < 2, max(L.second, R.second)+1);
}
class Solution {
public:
    bool isBalanced(TreeNode *root) {
        return judge(root).first;
    }
};

 方法二: 增加一个引用

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
bool judge(TreeNode *root, int& depth) {
    if(root == NULL) { depth = 0; return true; }
    int l, r;
    if(judge(root->left, l) && judge(root->right, r)) {
        depth = 1 + max(l, r);
        if(l-r <= 1 && l-r >= -1) return true;
        else return false;
    }
}
class Solution {
public:
    bool isBalanced(TreeNode *root) {
        int depth;
        return judge(root, depth);
    }
};

 

Maximum Depth of Binary Tree

 OJ: https://oj.leetcode.com/problems/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) {
        return root ? max(maxDepth(root->left), maxDepth(root->right))+1 : 0;
    }
};

 

33. Minimum Depth of Binary Tree && Balanced Binary Tree

标签:des   style   blog   http   io   for   ar   art   div   

原文地址:http://www.cnblogs.com/liyangguang1988/p/3939860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!