[Leetcode][Tree][Depth of Binary Tree]

计算树的深度

1、minimum depth of binary tree

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minDepth(TreeNode *root) {
13         if (root == NULL) {
14             return 0;
15         }
16         if (root->left == NULL && root->right == NULL) {
17             return 1;
18         }
19         int left = INT_MAX;
20         int right = INT_MAX;
21         if (root->left != NULL) {
22             left = minDepth(root->left);
23         }
24         if (root->right != NULL) {
25             right = minDepth(root->right);
26         }
27         return min(left, right) + 1;
28     }
29 };

总结:刚开始用非常简单的递归,发现wa了,是由于root结点在有一个子树的时候,不算是叶子,需要特殊处理。

又写了一个迭代的版本,也可以用作树的层次遍历。回忆了一下stack和queue的用法。

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minDepth(TreeNode *root) {
13         int result = INT_MAX;
14         queue<pair<TreeNode*, int>> q;
15         if (root != NULL) {
16             q.push(make_pair(root,1));
17         } else {
18             return 0;
19         }
20         while (!q.empty()) {
21             auto node = q.front().first;
22             auto depth = q.front().second;
23             q.pop();
24             if (node->left == NULL && node->right == NULL) {
25                 result = min(result, depth);
26             }
27             if (node->left != NULL) {
28                 q.push(make_pair(node->left, depth + 1));
29             }
30             if (node->right != NULL) {
31                 q.push(make_pair(node->right, depth + 1));
32             }
33         }
34         return result;
35     }
36 };

2、Maximum depth of binary tree

/**
 * 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 maxDepth(TreeNode *root) {
        if (root == NULL) {
            return 0;
        }
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
        /*
        if (root->left == NULL && root->right == NULL) {
            return 1;
        }
        int left = INT_MIN;
        int right = INT_MIN;
        if (root->left != NULL) {
            left = maxDepth(root->left);
        }
        if (root->right != NULL) {
            right = maxDepth(root->right);
        }
        return max(left, right) + 1;
        */
    }
};

CE了一次。。又不小心把单词拼错了。。

总结:maximum depth用了非常简单的递归,但是这种方法用在Minimum上就不行,需要对root结点进行特殊的处理。

[Leetcode][Tree][Depth of Binary Tree]

时间: 2024-08-25 21:45:37

[Leetcode][Tree][Depth of Binary Tree]的相关文章

[leetcode]_Maximum Depth of Binary Tree

第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 最大深度. 代码: public int maxDepth(TreeNode root) { if(root == null) return 0; int leftHeight = 1,rightHeight = 1; if(root.left != null) leftHeight += maxD

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://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,树为空,

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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

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

leetcode——Maximum Depth of Binary Tree (递归,)

Maximum Depth of Binary Tree Total Accepted: 59837 Total Submissions: 132940My 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 f

leetcode Maximum Depth of Binary Tree

代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int max; void deepSearch(TreeNode * root, int dep

[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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. Hide Tags Tree Depth-first Search 简单的深度搜索 #include <iostream> using namespace std; /** *

[leetcode] Minimum Depth of Binary Tree ,到叶子节点的最小距离 (python)

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. class Solution: # @param root, a tree node # @return an integer minDep =-1 def minDepth(se

leetcode Minimum Depth of Binary Tree C++题解

题目描述 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. 给出一个二叉树,求其最短路径 最短路径定义:从根节点到最近的叶子节点的路径上的节点个数 思路描述 这道题对于我来说引发的思考比较多 首先明确一个比较大但很重要的概念,二叉