The minimum depth of the 752-fork tree in the nine-chapter algorithm

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/76/


Topics

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.


Online test

http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/


Answer

Method One: Recursive.

This problem can be recursive method, a node one node of each node traversal, and at the same time the traversal of each node to record the corresponding number of layers, and then find the leaf node in the minimum layer is the answer we ask.

Method Two: Non-recursive.

The non-recursive method is to use the width first to search a layer of trees to traverse the nodes, when the first time to traverse the tree leaf node is the minimum depth of the two-fork tree we want to find.


Reference Code

http://www.jiuzhang.com/solutions/minimum-depth-of-binary-tree/

The minimum depth of the 752-fork tree in the nine-chapter algorithm

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.