重做104. Maximum Depth of 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.

思路:知道有recursion的方法。但是想应用一下dfs和backtracking,啊哈哈终于做出来了。

Solution1:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
        {
            return 0;
        }
        List<Integer> res=new ArrayList<Integer>();
        dfs(root,res,1);
        Collections.sort(res);
        return res.get(res.size()-1);
    }
    public void dfs(TreeNode root,List<Integer> res, int max)
    {
        if(root==null)
        {
            return;
        }
        if(root.left==null&&root.right==null)
        {
            res.add(max);
            return;
        }
        dfs(root.left,res,++max);
        max--;
        dfs(root.right,res,++max);
        max--;
    }

}

Solution2:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
        {
            return 0;
        }
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
    }
}

Recursion的定义. ref:http://www.cs.princeton.edu/courses/archive/fall10/cos126/lectures/07-23Recursion-2x2.pdf

Recursive Program Recursive Program. Implement a function having integer arguments by

• base case: Implementing it for some specific values of the arguments.

• reduction step: Assume the function works for smaller values of its arguments and use it to implement it for the given values

时间: 2024-10-14 08:07:18

重做104. Maximum Depth of Binary Tree的相关文章

Leetcode刷题记录[python]——104 Maximum Depth of Binary Tree

一.前言 对于这类抽象问题还有self用法,还要多做多练习,此题是参考其他答案所做. 二.题104 Maximum Depth of 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. # Definitio

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本题难度: Easy Topic: Binary Tree Description 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 f

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

[leetcode] 104. Maximum Depth of 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. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

[LeetCode] 104. Maximum Depth of Binary Tree Java

题目: 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. 题意及分析:找出一棵树的高度,即最深子节点.使用深度遍历的方法即可,用一个变量记录遍历到当前点的最大高度,然后当前点若有子节点,遍历到子节点,那么该点的高度+1和当前的最大

104. Maximum Depth of 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. public int MaxDepth(TreeNode root) { if(root == null) return 0; return Math.Max(MaxDepth(r

【LeetCode】104 - Maximum Depth of 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. Solution: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val;

leetCode 104.Maximum Depth of 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. 思路:很基础的一个题,DFS即可.代码如下: /** * Definition for a binary tree node. * public class TreeNode {

LeetCode OJ 104. Maximum Depth of 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. 很简单,直接上代码: 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * in