"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)"