获取树的所有结点值的和(N叉树)

思路

该问题的解决办法和标准的二叉树类似,遍历整个链表,并不断累加值,可以使用层序遍历或者递归来实现

代码

int findSum(TreeNode root){
    
    
  if(root == null)
    return 0;
  return root.getData()+findSum(root.getFirstChild())+findSum(root.getNextSibling());
}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/114988498