题解 | #牛群的最大高度#
牛群的最大高度
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;
public int findMaxHeight (TreeNode root) {
// write code here
vinTree(root);
return res;
}
void vinTree(TreeNode root){
if(root == null) return;
vinTree(root.left);
if(root.val > res) res = root.val;
vinTree(root.right);
}
}
本题属于树的遍历,通过一次遍历就可以得出结果,这里我使用的是中序遍历。
查看20道真题和解析
美团成长空间 2638人发布