赞
踩
class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; TreeNode leftnode = root.left; int leftdepth = 0; TreeNode rightnode = root.right; int rightdepth = 0; while(leftnode != null) { leftnode = leftnode.left; leftdepth++; } while(rightnode != null) { rightnode = rightnode.right; rightdepth++; } if(rightdepth == leftdepth) return (2 << rightdepth) - 1; else { int leftnum = countNodes(root.left); int rightnum = countNodes(root.right); return 1 + leftnum + rightnum; } } }
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
int leftdepth = minDepth(root.left);
int rightdepth = minDepth(root.right);
if(root.left == null && root.right != null) return 1 + rightdepth;
else if(root.left != null && root.right == null) return 1 + leftdepth;
else if(root.left == null && root.right == null) return 1;
else return 1 + Math.min(leftdepth, rightdepth);
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftheight = maxDepth(root.left);
int rightheight = maxDepth(root.right);
int height = 1 + Math.max(leftheight, rightheight);
return height;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。