"Leetcode-Interview algorithm classic-java Implementation" "111-minimum Depth of Binary tree (minimum depth of binary trees)"

Source: Internet
Author: User

"111-minimum Depth of Binary tree (minimum depth of binary trees)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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.

Main Topic

Given a two-fork tree to find the minimum depth of the tree.

Thinking of solving problems

Traversal method, traversing the entire tree to find the smallest depth.

Code Implementation

Tree result definition

publicclass TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) { val = x; }}

Algorithm implementation class

 Public  class solution {    Private intmin = Integer.max_value;//Minimum depth of record tree    Private intCur =0;//I attempt at the tree currently being processed     Public int mindepth(TreeNode Root) {depth (root);returnMin }/** * Depth of calculation tree * * @param node Current node */    Private void Depth(TreeNode node) {if(node = =NULL) {min = cur;return; } cur++;//current level of processing plus 1        //If it is a leaf node, and the path is smaller than the record minimum        if(Node.left = =NULL&& Node.right = =NULL&& cur < min) {min = cur;//Update minimum value}//Processing left subtree        if(Node.left! =NULL) {depth (node.left); }//Handle Right sub-tree        if(Node.right! =NULL) {depth (node.right); } cur--;//Restore}}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47414057"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "111-minimum Depth of Binary tree (minimum depth of binary trees)"

Related Article

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.