222. Count Complete Tree Nodes
Posted 咖啡中不塌缩的方糖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了222. Count Complete Tree Nodes相关的知识,希望对你有一定的参考价值。
Given a complete binary tree, count the number of nodes.
http://www.programcreek.com/2014/06/leetcode-count-complete-tree-nodes-java/
public int CountNodes(TreeNode root) { if(root == null) return 0; var loop = root; int height =0; while(loop != null) { loop = loop.left; height++; } int res = (int)Math.Pow(2,height)-1; int leftHeight =0; int rightHeight=0; var left = root.left; var right = root.right; while(left != null) { left = left.left; leftHeight++; } while(right != null) { right = right.right; rightHeight++; } if(leftHeight == rightHeight) return (int)Math.Pow(2,leftHeight+1)-1; return 1+CountNodes(root.left)+CountNodes(root.right); }
以上是关于222. Count Complete Tree Nodes的主要内容,如果未能解决你的问题,请参考以下文章
222. Count Complete Tree Nodes
222. Count Complete Tree Nodes
LeetCode222——Count Complete Tree Nodes