Leetcode--easy Series 6

Source: Internet
Author: User

#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 to the farthest leaf node.

Find the depth of the binary tree. Binary tree operation Many of them can be recursive

7ms/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */int maxDepth (struct T reenode* 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 was defined as a binary tree in which the depth of the Every node never differ by more than 1.

Determine whether a given binary tree is a balanced binary tree ... The depth difference is less than or equal to 1. The depth of each node can be recursively evaluated, and its subtree height difference is judged.

8ms/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */int maxDepth (struct T reenode* 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)//Empty tree        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 to the nearest leaf node.

finding the minimum depth---of binary tree
4ms/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */int mindepth (struct T reenode* 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 this 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           /   /           /  4         /  \              7    2      1

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

Determines whether the specified binary tree has a path and equals the specified value.

8ms/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */bool Haspathsum (Struc T 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);}



Leetcode--easy Series 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.