Maximum-depth-of-binary-tree&&minimum-depth-of-binary-tree

Source: Internet
Author: User

About binary tree, at present in Leetcode encountered two problems, together is the maximum depth of two fork tree, together is the minimum depth of two fork tree; about binary tree, the whole good take this opportunity to learn again;

Binary tree is a special kind of tree, in the binary tree each node has a maximum of two sub-nodes, commonly known as the Left Dial hand node and the right child node (or left child and right child), and the subtree of the binary tree has about the sub-tree, its order can not be arbitrarily reversed. The binary tree is a recursive definition, so the topic related to the two-fork tree can be solved by recursive thinking basically.

First topic:

Given a binary tree, find its maximum depth.

The depth of the binary tree is the distance from the root node to the farthest leaf node

Sample Example

Give a tree of the following two forks:

  1 / \ 2   3   /   4   5

The maximum depth of this binary tree is 3 .

This problem is solved by recursive thinking, but in order to ensure that we get the maximum depth, it should be the initial root left and right child nodes separated by recursion finally get the largest, the code is very simple.

Essence or depth first search;

1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9  *     }Ten  * } One  */ A  Public classSolution { -     /** -      * @paramroot:the root of binary tree. the      * @return: an integer. -      */ -      Public intmaxDepth (TreeNode root) { -         //Write your code here +         intleftdepth; -         intrightdepth; +         if(Root = =NULL ){ A             return0; at         } -         Else{ -Leftdepth = maxDepth (root.left) +1; -Rightdepth = maxDepth (root.right) +1; -             returnMath.max (leftdepth,rightdepth); -         } in          -     } to}
View Code

The second problem:

Given a binary tree, find its minimum depth.

The minimum depth of the binary tree is the distance from the root node to the nearest leaf node. Sample Example

Give a tree of the following two forks:

1

/     \

2 3

/    \

4 5

The minimum depth of this binary tree is 2.

At the beginning of this problem, it is easy to think that it is not very simple? Change the return value of the maximum depth to two inside the smallest one. But that's not true. Because if a single child node is empty in a binary tree, the return value will be 0, but the child node is not a leaf node.

1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9  *     }Ten  * } One  */ A  Public classSolution { -     /** -      * @paramroot:the root of binary tree. the      * @return: an integer. -      */ -      Public intmindepth (TreeNode root) { -         //Write your code here +         if(Root = =NULL)return0; -         intLeftdepth =mindepth (root.left); +         intRightdepth =mindepth (root.right); A         if(leftdepth==0| | Rightdepth ==0) {//The primary is to determine whether the left and right child nodes have a single empty case at             return1+rightdepth +leftdepth; -}Else { -             return1 +math.min (rightdepth,leftdepth); -         } -     } -}

Maximum-depth-of-binary-tree&&minimum-depth-of-binary-tree

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.