zoukankan      html  css  js  c++  java
  • [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

    The problem 1:

    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.

    My analysis:

    The recursion solution for this problem is very elegant!
    It include some skills we usually use in implenting recursive program for tree.
    1. the base case for tree traversal is the leaf node's empty child(null), not the leaf node itself.

    if (cur_root == null)
        return 0;

    2. when we calculate the path from current node to the leaf node, we calculate it from bottom to surface. At each node, we plus one, and return it to previous level recursion.
    3. We use Math.math() function to get the longest path for either sub-tree.

    return Math.max(helper(cur_root.left), helper(cur_root.right)) + 1; 

    My solution:

    public class Solution {
        public int maxDepth(TreeNode root) {
            return helper(root);
        }
        
        private int helper(TreeNode cur_root) {
            
            if (cur_root == null)
                return 0;
            
            return Math.max(helper(cur_root.left), helper(cur_root.right)) + 1; 
        }
    }

    The problem2: 

    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.

    My analysis:

    This problem is easy, but it differs a little with the previous problem: finding maximum path.
    In this problem, we need to tackle with the "leaf node case" very carefully.
    What's a leaf node?
    A leaf node must has no left child or right child. We should mix this case with situation that a node has only one child. Some errors I have commited to solve the problem.
    1. Directly return the minimum path among left-sub tree and right-sub tree. Apparently this is not right, what if a tree has only one left sub-tree with a length of 6?

    return Math.min(left_min, right_min) + 1;

    2. To fix the problem, I have tried to check if a node is a leaf node in each recursion. 

    private int helper(TreeNode cur_root) {
        if (cur_root == null)
            return 0;
        if (cur_root.left == null && cur_root.right == null) 
            return 1;
            ...
        return Math.min(left_min, right_min) + 1;
    }

    But this still does not work, because we must keep the checking condition "if (cur_root == null)". (if a node has only one child, the empty child must be tackled). Then the solution would aslo suffer from the wrong judgement as previous one.

    Fix:
    How about check if the current node has one child or two, then tackle and return the value respectively.
    1. check if the current node with only one child.
    Note: iff current node is a leaf node, the following stataments can still tackle it.

    if (left_min == 0)
        return right_min + 1;
    
    if (right_min == 0)
        return left_min + 1;

    2. if the current node with two children. 

    return Math.min(left_min, right_min) + 1;
    public class Solution {
        public int minDepth(TreeNode root) {
            
            if (root == null)
                return 0;
                
            return helper(root);
        }
        
        private int helper(TreeNode cur_root) {
            if (cur_root == null)
                return 0;
            
            int left_min = helper(cur_root.left);
            int right_min = helper(cur_root.right);
            
            if (left_min == 0)
                return right_min + 1;
            
            if (right_min == 0)
                return left_min + 1;
            
            return Math.min(left_min, right_min) + 1;
        }
    }
  • 相关阅读:
    12月14日 bs-grid , destroy_all()
    12月13日 什么是help_method,session的简单理解, find_by等finder method
    12月10日 render( locals:{...}) 传入本地变量。
    12月8日 周五 image_tag.
    12月7日,几个错误,拼写错误,遗漏符号:,记忆有误,max-width的作用。gem mini_magick, simple_form
    程序员必读之软件架构
    先进PID控制MATLAB仿真(第4版)
    中文版Illustrator CS6基础培训教程(第2版)
    Android系统级深入开发——移植与调试
    Excel在会计与财务管理中的应用
  • 原文地址:https://www.cnblogs.com/airwindow/p/4216027.html
Copyright © 2011-2022 走看看