"Leetcode" 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.
Recursive and non-recursive, this is relatively simple to mention. Breadth-first traversal can be. The key is how to maintain the depth of access.
Here are 4 kinds of code:
1 Importjava.util.ArrayList;2 Importjava.util.LinkedList;3 Importjava.util.List;4 ImportJava.util.Queue;5 6 classTreeNode {7 intVal;8 TreeNode left;9 TreeNode right;Ten OneTreeNode (intx) { Aval =x; - } - } the Public classMinimumdepthofbinarytree { - /** - * Recursive depth traversal - * @paramRoot + * @return - */ + Public intminDepth1 (TreeNode root) { A if(root==NULL) at return0; - if(root.left==NULL&&root.right==NULL){ - return1; - } - intleft=minDepth1 (root.left); - intright=minDepth1 (root.right); in if(left==0){ - returnRight+1; to } + if(right==0){ - returnLeft+1; the } * Else{ $ returnMath.min (right, left) +1;Panax Notoginseng } - } the Public intminDepth2 (TreeNode root) { + if(root==NULL) A return0; the intleft=minDepth1 (root.left); + intright=minDepth1 (root.right); - $ if(left==0&&right==0){ $ return1; - } - if(left==0){ theright=Integer.max_value; - }Wuyi if(right==0){ theleft=Integer.max_value; - } Wu - returnMath.min (right, left) +1; About $ } - - /** - * Breadth-first search. Once the leaf node is found, the traversal depth is returned. A * @paramRoot + * @return the */ - Public intminDepth3 (TreeNode root) { $ if(root==NULL){ the return0; the } theQueue<treenode>queue=NewLinkedlist<>(); the Queue.add (root); - intCount=queue.size ();//used to save access to the remaining non-accessible nodes of the current hierarchy. in intdepth=1;//the access depth used to hold the binary tree the while(!Queue.isempty ()) { the //Breadth Traversal AboutTreeNode topnode=Queue.poll (); thecount--; the if(topnode.left!=NULL){ the Queue.add (topnode.left); + } - if(topnode.right!=NULL){ the Queue.add (topnode.right);Bayi } the //leaf node found the if(topnode.left==NULL&&topnode.right==NULL){ - returndepth; - } the //access to the first layer is complete the if(count==0){ thedepth++; theCount=queue.size (); - } the the } the return0;94 } the /** the * Breadth-first search. Once the leaf node is found, the traversal depth is returned. the * @paramRoot98 * @return About */ - Public intMinDepth4 (TreeNode root) {101 if(root==NULL){102 return0;103 }104list<treenode>list=NewArraylist<>(); the intCount=1;106 List.add (root);107 while(List.size ()!=0){108list<treenode>singlelist=NewArraylist<>();109 for(TreeNode t:list) { the if(t.left!=NULL){111 Singlelist.add (t.left); the}if(t.right!=NULL){113 Singlelist.add (t.right); the } the if(t.left==NULL&&t.right==NULL){ the returncount; 117 }118 }119count++; -list=singlelist;121 }122 return0;123 }124 Public Static voidMain (string[] args) { theTreeNode RootNode1 =NewTreeNode (1);126TreeNode RootNode2 =NewTreeNode (2);127TreeNode RootNode3 =NewTreeNode (3); -TreeNode rootNode4 =NewTreeNode (4);129TreeNode ROOTNODE5 =NewTreeNode (5); theTreeNode RootNode6 =NewTreeNode (6);131TreeNode RootNode7 =NewTreeNode (7); theRootnode1.left =RootNode2;133Rootnode1.right =RootNode3;134Rootnode2.left =RootNode4;135Rootnode2.right =RootNode5;136Rootnode3.left =RootNode6;137Rootnode3.right =RootNode7;138Minimumdepthofbinarytree minimumdepthofbinarytree=NewMinimumdepthofbinarytree ();139 System.out.println (Minimumdepthofbinarytree.mindepth4 (RootNode1)); $ 141 }142 143}
Minimum depth Java for "Leetcode" Minimum Depth of binary tree Two forks