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 && Maximum Depth of Binary Tree的更多相关文章

  1. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  3. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  4. [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

    The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...

  5. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. [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 long ...

  7. Maximum Depth of Binary Tree 解答

    Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  8. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  9. (二叉树 BFS DFS) 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 long ...

随机推荐

  1. DAO JDBC 学生成绩管理系统

    1:student.course类 package JDBCU; public class Student { private String no; private String name; publ ...

  2. yii2-user

    https://github.com/dektrium/yii2-user 安装 : composer require "dektrium/yii2-user:0.9.*@dev" ...

  3. Linux-Big-Endian和Little-Endian转换

    转自:http://blog.csdn.net/aklixiaoyao/article/details/7548860 在各种计算机体系结构中,对于字节.字等的存储机制有所不同,因而引发了计算机通信领 ...

  4. 计算机网络(5)-----ICMP协议和PING程序

    控制报文协议(Internet Control Message Protocol) 定义 它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制消息是指网络通不通.主机是否可 ...

  5. 排序小结(C版)

    一.快速排序(C源码) #include <stdlib.h> #include <stdio.h> int adjust(int a[],int start,int end) ...

  6. 验证页面多个input文本的必填项

    前台页面 JS : function CheckMustWrite(){ var count = $("input[mustwrite = 'true']", document.f ...

  7. Redis持久化-数据丢失及解决(转载)

    本文转载自        Redis持久化-数据丢失及解决  感谢原作者 Redis的数据回写机制 Redis的数据回写机制分同步和异步两种, 同步回写即SAVE命令,主进程直接向磁盘回写数据.在数据 ...

  8. php大力力 [046节] 兄弟连高洛峰 PHP教程 2015年[最新最新最新最新最新]

    兄弟连高洛峰老师新版PHP视频教程列表[每日更新] http://bbs.lampbrother.net/read-htm-tid-160506.html HTML部分1.[2015]兄弟连高洛峰 H ...

  9. Python 基礎 - for流程判斷

    今天介紹另一個循環判斷式 for循環,首先,先寫一個很簡單的 for循環的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- for i in range ...

  10. Array.sort()方法

    Array.sort()方法将数组中的元素排序并返回排序后的数组. 当不带参数时,默认按照顺序排序,也就是从小到大.当然,也可以直接给sort加一个比较函数比较. ,,]; arr.sort(); c ...