二叉树常见算法.docxVIP

  • 8
  • 0
  • 约4.88千字
  • 约 4页
  • 2023-09-07 发布于天津
  • 举报
二叉树的常见算法: 二叉树的定义: /** DefinitionofTreeNode @authorZhy * */publicclassTreeNode{intval;TreeNodeleft;TreeNoderight;publicTreeNode(intval){this.val=val;} }获得二叉树的节点个数: /** method:recursionO(n)SystemStack〃递归 note:rootisnull:0; rootisnotnull,returnleft+right+1; @paramroot @return */ publicintgetNumberRec(TreeNoderoot){if(root==null)return0;returngetNumberRec(root.left)+getNumberRec(root.right)+1; } /** method:IterationO(n)Queue〃迭代 @paramroot @return */ publicintgetNum(TreeNoderoot){if(root==null)return0;intnum=1;LinkedListTreeNodequeue=newLinkedListTreeNode();queue.add(root);while(!queue.isEmpty()){T

文档评论(0)

1亿VIP精品文档

相关文档