题解 | #牛群的最大高度#
牛群的最大高度
https://www.nowcoder.com/practice/f745023c5ac641c9914a59377dacdacf
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型
*/
int res = 0;
int depth=0;
public int findMaxHeight (TreeNode root) {
// write code here
traverseMax(root);
return res;
}
public void traverseMax(TreeNode root){
if(root==null) return;
System.out.print(depth+" ");
traverseMax(root.left);
traverseMax(root.right);
depth = root.val;
res = Math.max(res, depth);
}
}
二叉树后序遍历,后序位置拿到节点的值,然后取最大的。
上海得物信息集团有限公司公司福利 1208人发布

查看5道真题和解析