/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*; public class Solution { public int run(TreeNode root) { if(root == null){ return 0; } Queue<TreeNode> s = new LinkedList<TreeNode>(); s.add(root); int start = 0; int end = 1; int level = 1; while(s!=null){ TreeNode node = s.poll(); start++; if(node.left == null && node.right == null){ return level; } if(node.left!=null){ s.add(node.left); } if(node.right!=null){ s.add(node.right); } if(start == end){ start = 0; level++; end = s.size(); } } return level; } }
原文地址:https://www.cnblogs.com/q-1993/p/10990601.html
时间: 2024-10-07 14:02:21