Leetcode--easy系列6

来源:互联网 发布:数据分析兼职平台 编辑:程序博客网 时间:2024/05/18 02:37

#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.

求二叉树的深度。二叉树的操作许多都可以用递归

//7ms/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) {    int l_depth,r_depth;    if(!root)        return 0;    else    {        l_depth = maxDepth(root->left);        r_depth = maxDepth(root->right);        return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1);    }        }

#110 Balanced 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.

判断给定二叉树是否是平衡二叉树。。。深度差小于等于1.可以递归求每个结点的深度,判断其子树高度差。

//8ms/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) {    int l_depth,r_depth;    if(!root)        return 0;    else    {        l_depth = maxDepth(root->left);        r_depth = maxDepth(root->right);        return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1);    }        }bool isBalanced(struct TreeNode* root) {    int k;    if(!root)//空树        return true;            k=maxDepth(root->left)-maxDepth(root->right);        if(abs(k)>1)        return false;    else        return isBalanced(root->left) && isBalanced(root->right);}

#111 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.

求二叉树的最小深度---
//4ms/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */int minDepth(struct TreeNode* root) {    int l_depth,r_depth;    if(!root)        return 0;    if(!root->left)        return 1+minDepth(root->right);    if(!root->right)        return 1+minDepth(root->left);    if(root->right && root->left)    {        l_depth = minDepth(root->left);        r_depth = minDepth(root->right);        return (l_depth < r_depth) ? (l_depth+1) : (r_depth+1);    }    }
#112 Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

判定指定二叉树是否存在路径和等于指定值。

//8ms/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */bool hasPathSum(struct TreeNode* root, int sum) {    if(!root)        return false;    if( root->left==NULL && root->right==NULL)        return sum == root->val;    else        return hasPathSum(root->left,sum - root->val) || hasPathSum(root->right,sum - root->val); }



0 0
原创粉丝点击