Binary Tree Series-binary tree depth, for example, [leetcode]

Source: Internet
Author: User

The concept of the depth of a binary tree is worth noting that it is the distance to the "leaf" node.

Generally, "depth" refers to the maximum depth, that is, the distance from the farthest leaf.

 

Here are two examples: Minimum depth and maximum depth.

1. Minimum Binary Tree depth

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     int minDepth(TreeNode *root) {13     }14 };

 

Because the depth must be the distance to the leaf node, We can't simply compare the recursive results of left and right Subtrees to return a smaller value using the depth traversal, for a node with a single child blank, the child with a blank child will return 0, but this node is not a leaf node, so the returned result is incorrect.

Therefore, if a single child of the currently processed node is null, a maximum value int_max is returned to prevent interference.

 1 class Solution { 2 public: 3     int minDepth(TreeNode *root) { 4         if(!root) return 0; 5         if(!root -> left && !root -> right) return 1;   //Leaf means should return depth. 6         int leftDepth = 1 + minDepth(root -> left); 7         leftDepth = (leftDepth == 1 ? INT_MAX : leftDepth); 8         int rightDepth = 1 + minDepth(root -> right); 9         rightDepth = (rightDepth == 1 ? INT_MAX : rightDepth);  //If only one child returns 1, means this is not leaf, it does not return depth.10         return min(leftDepth, rightDepth);11     }12 };

 

Of course, this question can also be done through hierarchical traversal.

class Solution {struct LevNode{    TreeNode* Node;    int Lev;};public:    int minDepth(TreeNode *root) {        if(NULL == root) return 0;        queue<LevNode> q;        LevNode lnode;        lnode.Node = root;        lnode.Lev = 1;        q.push(lnode);        while(!q.empty()){            LevNode curNode = q.front();            q.pop();            if(NULL == (curNode.Node) -> left && NULL == (curNode.Node) -> right)                return (curNode.Lev);            if(NULL != (curNode.Node) -> left){                LevNode newNode;                newNode.Node = (curNode.Node) -> left;                newNode.Lev = (curNode.Lev + 1);                q.push(newNode);            }            if(NULL != (curNode.Node) -> right){                LevNode newNode;                newNode.Node = (curNode.Node) -> right;                newNode.Lev = (curNode.Lev + 1);                q.push(newNode);            }        }        return 0;    }};

 

For this question, the leetcode solution takes 48 Ms.

 

2. Maximum depth of a binary tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

The maximum depth is also the length of the leaf node, but because it is the maximum depth, a non-leaf node with a single child blank will not interfere with the results, so it can be done in the most concise way.

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int maxDepth(TreeNode *root) {        if(!root) return 0;        int leftDepth = maxDepth(root -> left) + 1;        int rightDepth = maxDepth(root -> right) + 1;        return max(leftDepth, rightDepth);    }};

 

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.