Leetcode Minimum Depth of Binary Tree C + +

Source: Internet
Author: User

Title Description

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.

Give a binary tree, find its shortest path

Shortest path Definition: The number of nodes on the path from the root node to the nearest leaf node

Idea Description

This problem is a lot of thinking for me.

First, a large but important concept is defined, and the binary tree is a recursive data structure, so it is an excellent structure for examining the recursive thinking ability.
Recursion must be a depth-first search

First, recall the code to find the height of the binary tree.

int treeHeight(TreeNode *root){    if (root==NULL)        0;    intleft=treeHeight(root->left);    intright=treeHeight(root->right);    1+max(left,right);}

Analogy to this code so I only changed the above line of code to commit

Return 1+min (left,right);

So WA, because the result is not to consider whether the leaf node has calculated its depth, the problem requires us only in the leaf node to determine the depth, as to how to determine whether it is a leaf node, recursive to the node is empty, to determine whether it has a sibling node, if not, that the empty node's parent node is a leaf node , so that the depth of the node is 0, its parent node is naturally 1, if there is a sibling node, that is, the empty node's parent node is not a leaf node, so that the depth of the node is positive infinity (Int_max). The comparison of the depth of non-leaf nodes is avoided. The specific code is implemented as follows

Class Solution { Public: int mindepth (TreeNode*Root) {returnMindepth (Root,false); }Private: int mindepth (TreeNode*Root,bool hasbrother) {if(Root==NULL)        {returnHasbrother?Int_max:0; }return 1+min(Mindepth (Root -Left,root -Right!=NULL), mindepth (root -Right,root -Left!=NULL)); }};

Again, some understanding of the recursion of the tree, the recursion of the tree is the code sequence to a recursive place and then always recursive to the deepest, and then step to the upper level after the return of the values to continue to execute sequentially.

The next is a non-recursive solution, traversing the tree, counting the depth of each leaf node, updating the minimum value, and paying attention to determine whether or not to continue traversing in the course of the traversal in order to prune. The code is implemented as follows:

classSolution { Public:intMindepth (TreeNode *root) {if(Root==null)return 0;intResult=int_max; Stack<pair<treenode *,int>> s;//apply for a stack of memory node depth pairsS.push (Make_pair (Root,1));//press into the root node         while(!s.empty ())//Similar sequence traversal tree, but does not follow strict left-to-right, traverse to a node and then move it around the son into a stack{AutoNode=s.top (). First;AutoDepth=s.top (). Second; S.pop ();if(Node->left==null && Node->right==null)//per-leaf node updateResult=min (result,depth);if(Node->left && Depth<result)//depth control, pruning, without traversing to each nodeS.push (Make_pair (node->left,depth+1));if(Node->right && Depth<result) s.push (Make_pair (node->right,depth+1)); }returnResult }};

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

Leetcode Minimum Depth of Binary Tree C + +

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.