牛客网编程-二叉树的深度(java)

xiaoxiao2021-02-28  88

思路:求二叉树的深度,想到左右子树递归

代码:

/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public int TreeDepth(TreeNode root) { if(root == null){ return 0; } int deepth = 1; int left = this.TreeDepth(root.left); int right = this.TreeDepth(root.right); deepth += left > right?left:right; return deepth; } }end

转载请注明原文地址: https://www.6miu.com/read-34985.html

最新回复(0)