作者:安小辰 | 来源:互联网 | 2024-11-29 14:26
https:leetcode-cn.comproblemscount-complete-tree-nodes递归的题目,左右中的后序遍历思想。***Definiti
https://leetcode-cn.com/problems/count-complete-tree-nodes/
递归的题目,左右中的后序遍历思想。
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0; int leftNum = countNodes(root.left); int rightNum = countNodes(root.right); int count = leftNum + rightNum + 1; return count;}
}