博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Maximum Depth of Binary Tree
阅读量:6269 次
发布时间:2019-06-22

本文共 3420 字,大约阅读时间需要 11 分钟。

我自己写的divide&conquer

/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /**     * @param root: The root of binary tree.     * @return: An integer.     */    public int maxDepth(TreeNode root) {        // write your code here        if (root == null) {            return 0;  //?        }        int result = divide(root);        return result;    }    private int divide(TreeNode root) {        if (root == null) {            return 0;        }        int re1 = divide(root.left);        int re2 = divide(root.right);        if (re1 >= re2) {            return re1 + 1;        } else {            return re2 +1;        }    }}
View Code

九章

// Version 1: Divide Conquerpublic class Solution {    public int maxDepth(TreeNode root) {        if (root == null) {            return 0;        }        int left = maxDepth(root.left);        int right = maxDepth(root.right);        return Math.max(left, right) + 1;    }}

递归老写错,摊手╮(╯-╰)╭

正确的:我抄的答案

/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left = this.right = null;* }* }*/public class Solution {/*** @param root: The root of binary tree.* @return: An integer.*/    private int result;    public int maxDepth(TreeNode root) {    // write your code here        result = 0;   //这里不能写int result 不能重新定义        traverse(root, 1);//传入1不是0        return result;    }    private void traverse(TreeNode root, int curr) {        if (root == null) {        return ;        }                if (curr > result) { //先写这个            result = curr;        }        traverse(root.left, curr + 1);        traverse(root.right, curr + 1);    }}
View Code

九章

// version 2: Traverse/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /**     * @param root: The root of binary tree.     * @return: An integer.     */    private int depth;        public int maxDepth(TreeNode root) {        depth = 0;        helper(root, 1);                return depth;    }        private void helper(TreeNode node, int curtDepth) {        if (node == null) {            return;        }                if (curtDepth > depth) {            depth = curtDepth;        }                helper(node.left, curtDepth + 1);        helper(node.right, curtDepth + 1);    }}
View Code

错的

/**

* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if (root == null) {
return 0; //?
}
int result = 0;
traverse(root, result);
return result;
}
private void traverse(TreeNode root, int result) {
if (root == null) {
return;
}
int re1 = 0;
int re2 = 0;
traverse(root.left, re1);
traverse(root.right, re2);
if (re1 >= re2) {
result = re1 + 1;
} else {
result = re2 +1;
}

}

}

转载于:https://www.cnblogs.com/yunyouhua/p/6719887.html

你可能感兴趣的文章
常见web攻击方式
查看>>
hdu 4472
查看>>
oracle存储过程中is和as区别
查看>>
windows 2003 群集
查看>>
几个gcc的扩展功能
查看>>
Spark一个简单案例
查看>>
关于结构体占用空间大小总结(#pragma pack的使用)
查看>>
通过浏览器查看nginx服务器状态配置方法
查看>>
shell简介
查看>>
android 使用WebView 支持播放优酷视频,土豆视频
查看>>
怎么用secureCRT连接Linux
查看>>
C# 使用WinRar命令压缩和解压缩
查看>>
linux学习笔记一----------文件相关操作
查看>>
Mono for Android 优势与劣势
查看>>
服务器端开发技术
查看>>
Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
查看>>
ajax提交多个对象,使用序列化表单和FormData
查看>>
深入分析由前序和中序重构二叉树问题
查看>>
leetcode 题解 || Valid Parentheses 问题
查看>>
将图片转成base64字符串并在JSP页面显示的Java代码
查看>>